Hi,
This is what i am trying to do. I have a form that i am setting up to run a query by command button, depending on the fields chosen in the form.
i need to use fields from two tables in this form. In the sql code, I used a string variable to hold the table name then insert it in select statement. I may be way off on this i am still a newbie in sql.
the code reads:
Private Sub cmdRunQuery_Click()
Dim db As DAO.Database
Dim QD As QueryDef
Dim where As Variant
Dim tblname1 As String
Dim tblname2 As String
Set db = CurrentDb()
On Error Resume Next
db.QueryDefs.Delete ("Dynamic_Query"
On Error GoTo 0
where = Null
'InputData Table info
where = where & " and [tblInputData].[ParcelNo] = ' " + Me![ParcelNo] + "'"
tblname1 = tblInputData
' many more fields, each with the tblname1 / tblname2 stating which table is used
If Left(Me![ParcelNo], 1) = "*" Or Right(Me![ParcelNo], 1) = "*" Then
where = where & " AND [tblInputData].[ParcelNo] like ' " + Me![ParcelNo] + "'"
Else
where = where & " AND [tblInputData].[Parcelno] = ' " + Me![ParcelNo] + "'"
End If
tblname1 = tblInputData
' many more numeric ranges with same set up
' I cannot get this statement correct >> i need to add the tblname variables and am not sure how to go about it????
MsgBox "Select * from" tblname1 & ("where" + Mid(where, 6) & ";"
'Set QD = db.CreateQueryDef ("Dynamic_Query", select * from" tblname1 & (" where " + Mid(where, 6) & ";")
DoCmd.OpenQuery "Dynamic_Query"
End Sub
WIth the last bit of code from the Message box on is not compiling correctly
the error is expected end of statement
ANy ideas????
THanks in advance
This is what i am trying to do. I have a form that i am setting up to run a query by command button, depending on the fields chosen in the form.
i need to use fields from two tables in this form. In the sql code, I used a string variable to hold the table name then insert it in select statement. I may be way off on this i am still a newbie in sql.
the code reads:
Private Sub cmdRunQuery_Click()
Dim db As DAO.Database
Dim QD As QueryDef
Dim where As Variant
Dim tblname1 As String
Dim tblname2 As String
Set db = CurrentDb()
On Error Resume Next
db.QueryDefs.Delete ("Dynamic_Query"
On Error GoTo 0
where = Null
'InputData Table info
where = where & " and [tblInputData].[ParcelNo] = ' " + Me![ParcelNo] + "'"
tblname1 = tblInputData
' many more fields, each with the tblname1 / tblname2 stating which table is used
If Left(Me![ParcelNo], 1) = "*" Or Right(Me![ParcelNo], 1) = "*" Then
where = where & " AND [tblInputData].[ParcelNo] like ' " + Me![ParcelNo] + "'"
Else
where = where & " AND [tblInputData].[Parcelno] = ' " + Me![ParcelNo] + "'"
End If
tblname1 = tblInputData
' many more numeric ranges with same set up
' I cannot get this statement correct >> i need to add the tblname variables and am not sure how to go about it????
MsgBox "Select * from" tblname1 & ("where" + Mid(where, 6) & ";"
'Set QD = db.CreateQueryDef ("Dynamic_Query", select * from" tblname1 & (" where " + Mid(where, 6) & ";")
DoCmd.OpenQuery "Dynamic_Query"
End Sub
WIth the last bit of code from the Message box on is not compiling correctly
the error is expected end of statement
ANy ideas????
THanks in advance