I'm having trouble figuring out how to use an if-then statement to check whether or not a variable is pointing to a look-up column.
What I have is a form that contains a combobox and a text box. The combobox contains the list of fields available in my table. When a field is selected, the user then enters text in to the text box. These two inputs create a query.
The problem is that some of my fields are look-up columns, and some are not. If I insert ".value" into the SQL, it works great IF the field is a look-up column, but not if it's just a regular field. If I remove the ".value", it works great IF the field is a regular field, but not if it is a look-up column.
So is there a way to say:
If a = look-up column then
use this SQL
Else
use this SQL
End If
??
Here's the code:
Expand|Select|Wrap|Line Numbers
- Private Sub Command0_Click()
- On Error Resume Next
- Dim dbs As Database
- Dim Rs As DAO.Recordset
- Dim a As String
- Dim b As String
- Dim strSQL As String
- Dim strQueryName As String
- Dim qryDef As QueryDef
- Set dbs = CurrentDb
- a = Me.Combo1
- b = Me.Text7
- strQueryName = "myQuery"
- dbs.QueryDefs.Delete strQueryName
- If '**What do I put here?** Then
- 'lookup column
- strSQL = "SELECT ProjectRegForm.*, ProjectRegForm." & a & " FROM ProjectRegForm WHERE (((ProjectRegForm." & a & ".Value)LIKE '*" & b & "*'))"
- Else
- 'not lookup column
- strSQL = "SELECT ProjectRegForm.*, ProjectRegForm." & a & " FROM ProjectRegForm WHERE (((ProjectRegForm." & a & ")LIKE '*" & b & "*'));"
- End If
- Set qryDef = dbs.CreateQueryDef(strQueryName, strSQL)
- DoCmd.OpenQuery "myQuery", acViewNormal
- End Sub
Cheers,
Jason