Where Clause from list box in Insert Query.

  • Thread starter Thread starter todd macphee
  • Start date Start date
T

todd macphee

Still giving me a syntax error?

-----Original Message-----
"WHERE tblhours.[txtname]= '" & Driver & "' AND
[tblhours].[dtmdate] Between #" & Me.txtStartDate & "# and
#" & Me.txtEndDate & "#'"

You need single quotes around Driver.

--
Paul Overway
Logico Solutions, LLC
www.logico-solutions.com


Hello, and thank you to anyone who can help with this. I
am trying to struggle through programming databases with
vb on my own, and I'm finding it hard.

I have a form, with a listbox, which is populated by
seting its rowsource property to another list box. I have
the MultiSelect property set to Simple, which is why i
used the counter. The following Code is meant to retreive
the autonumbered primary key for each record, based on
criteria set by the selection(s) from the list box, and 2
text fields with start and end dates in them.

'// Clear The Table for the Matches
Call CurrentDb.Execute("Delete * from TblTempReport") '//
Populate It Dim counter As Long Dim sql As String Dim
Driver As String
Call DoCmd.Hourglass(True)

For counter = 0 To Me.lstDrivers.ItemsSelected.Count -
1
Driver = Me.lstDrivers.ItemData
(lstDrivers.ItemsSelected(counter))

'// fill the temp table...
sql = "INSERT INTO tbltemp (numrecordnumber)
SELECT [tblhours].[aunrecordnumber]" & _
"FROM [tblhours] " & _
"WHERE tblhours.[txtname]= " & Driver & " and
[tblhours].[dtmdate] between #" & Me.txtStartDate & "# and
#" & Me.txtEndDate & "#'"

Call CurrentDb.Execute(sql)
Next counter

Call DoCmd.Hourglass(False)


It wont work though, I keep getting an error with the
message " Syntax Error (missing Operator) in query
expression."

Thoughts ?

Todd


.
..
 
Hi,

Force a US format for the dates (and be sure to have at least one space
around each keyword):


"... BETWEEN " & Format(Me.txtStartDate, "\#mm-dd-yyyy\#") & " AND ...



You probably need quotes around the text:


"... tblhours.[txtname]= """ & Replace(Driver, """", """""") & """ AND
.... "
1 3 4
6 3 1


where I assume you use VBA6 (Replace is then natively defined in that
version). The number indicate the number of " in the line above them.


Debug.Print the string, before executing it, and, if required, cut and
paste it in a SQL view of a new query. That may help to see the problem, if
there is still a problem.


Continue on the same thread, if possible, that helps us to keep the
whole context in place, rather than starting all over again (in most cases,
that what may happen if you start a new thread).



Hoping it may help,
Vanderghast, Access MVP



todd macphee said:
Still giving me a syntax error?

-----Original Message-----
"WHERE tblhours.[txtname]= '" & Driver & "' AND
[tblhours].[dtmdate] Between #" & Me.txtStartDate & "# and
#" & Me.txtEndDate & "#'"

You need single quotes around Driver.

--
Paul Overway
Logico Solutions, LLC
www.logico-solutions.com


Hello, and thank you to anyone who can help with this. I
am trying to struggle through programming databases with
vb on my own, and I'm finding it hard.

I have a form, with a listbox, which is populated by
seting its rowsource property to another list box. I have
the MultiSelect property set to Simple, which is why i
used the counter. The following Code is meant to retreive
the autonumbered primary key for each record, based on
criteria set by the selection(s) from the list box, and 2
text fields with start and end dates in them.

'// Clear The Table for the Matches
Call CurrentDb.Execute("Delete * from TblTempReport") '//
Populate It Dim counter As Long Dim sql As String Dim
Driver As String
Call DoCmd.Hourglass(True)

For counter = 0 To Me.lstDrivers.ItemsSelected.Count -
1
Driver = Me.lstDrivers.ItemData
(lstDrivers.ItemsSelected(counter))

'// fill the temp table...
sql = "INSERT INTO tbltemp (numrecordnumber)
SELECT [tblhours].[aunrecordnumber]" & _
"FROM [tblhours] " & _
"WHERE tblhours.[txtname]= " & Driver & " and
[tblhours].[dtmdate] between #" & Me.txtStartDate & "# and
#" & Me.txtEndDate & "#'"

Call CurrentDb.Execute(sql)
Next counter

Call DoCmd.Hourglass(False)


It wont work though, I keep getting an error with the
message " Syntax Error (missing Operator) in query
expression."

Thoughts ?

Todd


.
.
 
Back
Top