Re: Data type mismatch in criteria expression



Seth,

My first rule in writing SQL expressions is "keep it simple"; this practically means, strip out anything unnecessary that Access puts in the SQL expression of a query. In this case, this includes (a) repeating the table name before the field names (not needed since there is only one table in the FROM clause), and (b) getting rid of redundant parentheses around criteria in the WHERE clause. Applying those, the SQL expression becomes quite simple to follow:

strSQLDeleteStd = "DELETE * FROM tblTestRprtStds " _
    & "WHERE fldTestRprtStdID = '" & Me!txtTestRprtID & "' " _
    & "AND fldTestrprtStdRefNum= '" & intStdId & "'"

Now, how it works:

An SQL expression in VBA code is essentially a string expression; this is what a DoCmd.RunSQL or CurrentDb.Execute expects as an argument. Therefore, it is enclosed in double quotes, to denote the value assigned to the strSQLDeleteStd variable as such (string). In other words, double quotes denote String.

An expression can be broken up into several lines for readability; to denote it is continued over to the next line, a " _" (space and underscore, without the quotes) is add ed at the end.
A string can be made up of parts, concatenated with ambersands.
The two previous points explain how space-underscores and ambersands are used to put together a string expression, made up of parts extending over several lines.


Some parts of the expression are left outside the double quotes; this tells VBA that those are actually variables, functions or references to other database objects (form textboes, in this case) so VBA "reads" their value and concatenates the value into the string.

The single quotes denote that the text between them within the overall string, is to be treated as a string on its own right; they are used where you would normally have double quotes in the query SQL. For instance, if part of) your query SQL was:
WHERE CustName = "Smith"
You would use single quotes around Smith in the SQL string in VBA:
strSQL = "SELECT Somefield FROM Table1 WHERE CustName = 'Smith')


With the above in mind, the syntax proposed at the beginning assumes that both fldTestRprtStdID and fldTestrprtStdRefNum are type Text; otherwise, the single quotes around the respective control references should be removed. For instance, if the latter is numeric, the expression should become:

strSQLDeleteStd = "DELETE * FROM tblTestRprtStds " _
    & "WHERE fldTestRprtStdID = '" & Me!txtTestRprtID & "' " _
    & "AND fldTestrprtStdRefNum= " & intStdId

HTH,
Nikos

.



Relevant Pages

  • Re: Update existing values incrementally w/UPDATE SQL
    ... and in the book; Access 2007 VBA Programmer's Reference. ... As far as quotes go, this was one of the trickiest ... Consider vba's interpretation of a string: ... when an SQL is processed (I use an SQL example because it is the most ...
    (microsoft.public.access.modulesdaovba)
  • Re: Update existing values incrementally w/UPDATE SQL
    ... pretend that Me.txtString is a control on your form, ... You cant put Me.txtString inside the double quotes, or VBA just reads it as a ... and in the book; Access 2007 VBA Programmer's Reference. ... when an SQL is processed (I use an SQL example because it is the most ...
    (microsoft.public.access.modulesdaovba)
  • Re: Data type mismatch in criteria expression
    ... Applying those, the SQL ... > to the strSQLDeleteStd variable as such (string). ... > quotes denote String. ... > where you would normally have double quotes in the query SQL. ...
    (microsoft.public.access.queries)
  • Re: using VBA to insert records from access database to sql server
    ... In SQL you 'escape' single quotes by doubling them. ... > Dim oConn As ADODB.Connection ... > Dim strInsert As String ...
    (microsoft.public.access.modulesdaovba)
  • Re: Passing Date Values - Beginner
    ... Everything in a SQL Statement is text. ... > I'm not sure how you constructed your query, ... > reference a variable by name in your code (e.g. Dim s As String) you will ... > note that you don't put quotes around it. ...
    (microsoft.public.dotnet.framework.aspnet)