VBA SQL Select statement based on content in a text box

shabbaranks

Registered User.
Local time
Today, 04:44
Joined
Oct 17, 2011
Messages
300
Ok guys - yep me again. Im bambuzzled as to how VBA and sql statements are supposed to work. It seems you use different command depending on what you are trying to achieve - is this correct?

What I am trying to do is query a table based on the input of a textbox. Based on below the error U get is Syntax error (missing operator) in query expression '[ProjectRef] = & Me.ProjectRef_TotalHours &'.

Code:
Private Sub TotalHoursOk_btn_Click()
Dim MySQL As String
MySQL = "Select * From TimesheetTable WHERE [ProjectRef] = & Me.ProjectRef_TotalHours & "
DoCmd.SetWarnings False
DoCmd.RunSQL MySQL
DoCmd.SetWarnings True
End Sub

What am I doing wrong?
 
Howzit

You want (on the basis it is a number)

Code:
MySQL = "Select * From TimesheetTable WHERE [ProjectRef] =" & Me.ProjectRef_TotalHours
 
Last edited:
Thanks for the quick reply, its not a number its a text field, but I do this I get the error a RunSQL action requires an argument consistsing of an SQL statement.

I'll give it an argument in a minute :)
 
Last edited:
Howzit

It may be a text field, but what is teh data type of the text field. If it is a number then the syntax is what I said, but if it is Text then it would be:

Code:
MySQL = "Select * From TimesheetTable WHERE [ProjectRef] ='" & Me.ProjectRef_TotalHours & "'"

In saying that, to run that RunSQL command the SQL statement needs to be an action statement (Delete \ Update or Append), not a Select statement that you have.

To get around this I have used the following method to run a Select statement

Create a Public Module and a query called qryQueries (any valid Select stmt is fine)

Code:
Public Sub QDef(strSql As String)

    Dim db As DAO.Database
    Dim qdfTemp As QueryDef
    Dim strSeason As String
    Dim strDocName As String
    

    Set db = CurrentDb

    strDocName = "qryQueries"



    db.QueryDefs.Delete strDocName
    Set qdfTemp = db.CreateQueryDef("qryQueries")
    qdfTemp.sql = strSql

    DoCmd.OpenQuery strDocName


End Sub

Then your code would be:

Code:
MySQL = "Select * From TimesheetTable WHERE [ProjectRef] ='" & Me.ProjectRef_TotalHours & "'"
QDef(MySQL)
 
Thanks for that, works like a treat. Although is there a way I can get it to accept wildcards for the criteria within the text box? For example I have multiple records which start PP190 and then - test, -record, so they would read PP190-test, PP190-record etc but if I do a search with PP190* it returns nothing - any ideas?

Thanks
 
Howzit

Try

Code:
MySQL = "Select * From TimesheetTable WHERE [ProjectRef] LIKE '*" & Me.ProjectRef_TotalHours & "*'"
 
Cool, thanks worked like a charm.

You couldnt help me with this code please?

Code:
Private Sub TotalHoursOk_btn_Click()
Dim MySQL As String
Dim sGroupID As String
sUser = Forms![TimesheetForm]![LoggedInUser]
sGroupID = DLookup(DepGroupID, UserNames_tbl, sUser)
If sGroupID = "2" Then
MySQL = "Select * From TimesheetTable WHERE [ProjectRef] LIKE '*" & Me.ProjectRef_txtbox & "*'"
QDef (MySQL)
Else
End If
End Sub

Im trying to do it step by step. Am I correct that the above code is looking within UserNames_tbl and checking sUser against the current logged in user which is in the TimesheetForm. And if sUser has a number 2 in thier DepGroupId column then it will do the select statement?

Thanks for all your help :)
 
Howzit

I think this part needs to change

Code:
sGroupID = DLookup(DepGroupID, UserNames_tbl, sUser)
to

Code:
sGroupID = DLookup(DepGroupID, UserNames_tbl, "[Userid]='" & sUser & "'")

You need to tell the dlookup which field to compare the sUser string to. You will need to use your own fieldname - i have used "Userid"
 
Thanks Ive made those changes, and also Im sure sUserID should be integer as its a stored number as thats what it is in the table? so I have changed that. But now I get error type mismatch :S



Code:
Option Compare Database
Private Sub TotalHoursOk_btn_Click()
Dim MySQL As String
Dim sGroupID As String
Dim sUserID As Integer
sUserID = Forms![TimesheetForm]![LoggedInUser]
sGroupID = DLookup(DepGroupID, UserNames_tbl, "[sUser]='" & sUserID & "'")
If sGroupID = "2" Then
MySQL = "Select * From TimesheetTable WHERE [ProjectRef] LIKE '*" & Me.ProjectRef_txtbox & "*'"
QDef (MySQL)
Else
End If
End Sub
 
Howzit

Ok - try this - I treated the UserID as a string, so now I have removed teh apostophes.

Code:
sGroupID = DLookup(DepGroupID, UserNames_tbl, "[sUser]=" & sUserID)
 
Just so I have got this straight as I might be leading you up the garden path. Is my understanding of this right

The sUserID is pulled from the form and stored. The DLookup is then looking for the sUserID in the UserNames_tbl and pulling out the relevant DepGroupID for that user based on the sUSerID?

There are only 3 different numbers possible and in this case I am looking for number 2.

Code:
Dim sGroupID As Integer
Dim sUserID As String
sUserID = Forms![TimesheetForm]![LoggedInUser]
sGroupID = DLookup(DepGroupID, UserNames_tbl, "[DepGroupID]=" & sUserID)


Sorry if I am confusing the situation, but Im litterally getting my head around this as we speak :) or trying anyway :)
 
Howzit

You have this wrong

Code:
sGroupID = DLookup(DepGroupID, UserNames_tbl, "[DepGroupID]=" & sUserID)
You need what we have in the previous post

Code:
sGroupID = DLookup(DepGroupID, UserNames_tbl, "[sUser]=" & sUserID)

This will return the DepFroupID from table UserNames_tbl where the SUser is the user in the form Forms![TimesheetForm]![LoggedInUser].
 
Hi guys,

Im still having a few issues with this, as per the code below Im getting the error " You entered an invalid argument in a domain aggregate function". So I have used the Immediate window to query what sGroupID is and its returning 0 but its 2 in the table.

I even changed the DLookup to point directly to the LoggedInUser and still get the same result -sGroupID - 0 when it should be 2.

Code:
Dim sGroupID As Integer
 
sGroupID = DLookup(DepGroupID, UserNames_tbl, "[sUser] = " & Forms![TimesheetForm]![LoggedInUser])
 
Howzit
Sorry my mistake - somehow got rid of quotes. Try
Code:
     sGroupID = DLookup("[DepGroupID]", "UserNames_tbl", "[sUser]=" & sUserID)
 
Hmmm its still causing issues. Its not using the sUserID which is the LoggedInUser to lookup the value for the sGroupID from the table. Its referencing the sUserID

Code:
Dim MySQL As String
Dim sGroupID As Integer
Dim sUserID As String

sUserID = Forms![TimesheetForm]![LoggedInUser]
sGroupID = DLookup("DepGroupID", "UserNames_tbl", "[sUser]=" & sUserID)
If sGroupID = "2" Then
MySQL = "Select * From TimesheetTable WHERE [ProjectRef] LIKE '*" & Me.ProjectRef_txtbox & "*'"
QDef (MySQL)
Else
MsgBox "You are only allowed to query your own hours", vbInformation
Me.TotalHours_Combo.SetFocus
End If
End Sub
Obviously its not right as it would work - but I cant see what the problem is??

Ta
 
Solved - put quotes around the sUserID to read

Code:
sUserID = "Forms![TimesheetForm]![LoggedInUser]"

And ot works fine now.... Not too sure why :confused:
 
Howzit
Sorry away on business and don't have good access to help. Well done
 

Users who are viewing this thread

Back
Top Bottom