I am trying to learn a little VBA and am stumped again...
I am trying to build a SQL statement and query from an input form. I am using a book and I am not understanding or it is not explaining well.
In the VBA form module I have 2 functions and then one function in a module called module 1. On clicking a button on the form a query and sql statement should be created based on the data in some fields (only one right now).
When I click the button nothing happens - I just dont see how one the click function calls the SQL builder or query writer.
If anyone can point me right I would appreciate it.
Thanks
and in module1 I have
I am trying to build a SQL statement and query from an input form. I am using a book and I am not understanding or it is not explaining well.
In the VBA form module I have 2 functions and then one function in a module called module 1. On clicking a button on the form a query and sql statement should be created based on the data in some fields (only one right now).
When I click the button nothing happens - I just dont see how one the click function calls the SQL builder or query writer.
If anyone can point me right I would appreciate it.
Thanks
Code:
Private Sub Command17_Click()
Dim StrSQL As String
If Not EntriesValid Then Exit Sub
If Not BuildSQLString(StrSQL) Then
MsgBox "There was a problem Building the SQL string"
Exit Sub
End If
MsgBox StrSQL
CurrentDb.QueryDefs("qryExample").SQL = StrSQL
End Sub
__________________________________________
Function BuildSQLString(StrSQL As String) As Boolean
Dim strSELECT As String
Dim strFROM As String
Dim strWHERE As String
strSELECT = " s.* "
strFROM = "tblUnit4Data s "
If CheckClass Then
strWHERE = strWHERE & " AND s.CLASS = " & ComboClass1
End If
StrSQL = "SELECT " & strSELECT
StrSQL = StrSQL & "FROM " & strFROM
If srtWHERE <> " " Then StrSQL = StrSQL & "WHERE " & Mid$(strWHERE, 6)
MsgBox StrSQL
BuildSQLString = True
End Function
and in module1 I have
Code:
Function MakeQueryDef(StrSQL As String) As Boolean
Dim qdf As QueryDef
If StrSQL = "" Then Exit Function
Set qdf = CurrentDb.CreateQueryDef("qryExample")
qdf.SQL = StrSQL
qdf.Close
RefreshDatabaseWindow
MakeQueryDef = True
End Function