Question : how do I run a SELECT query in VBA MS Access

I need the ability to check a input box against the field it fills on a Access table. If there are duplicates I need to show them, but not stop from entering. everything seems to be working except the query pops up and ask for a parameter instead of using the one pulled from the txt box.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
Private Sub txtLastName_BeforeUpdate(Cancel As Integer)
 
Dim mResponse As Integer
Dim mySql As String
Dim sWhere As String
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim tbx As String
 
tbx = (Me.txtLastName)
 
mySql = "SELECT myTBL.LastName FROM myTBL "
  
sWhere = "WHERE myTBL.LastName = " & tbx
  
 
mySql = mySql & sWhere
 
Set db = CurrentDb
Set qdf = db.QueryDefs("qryProject")
 
qdf.SQL = mySql
qdf.Close
 
  If (Me.txtLastName) = [myTBL.LastName] Then
        
       mResponse = MsgBox("Duplicate Entries Detected!", vbYesNo)
        
        
        If mResponse = vbYes Then
        
           
         
         DoCmd.OpenQuery "qryProject", acViewNormal
        
      
         
        Else
            
            Exit Sub
                          
            
        End If
        
   End If
 End Sub
Open in New Window Select All

Answer : how do I run a SELECT query in VBA MS Access

ok i see that you are modifying the  querydef

just use this

sWhere = "WHERE myTBL.LastName = Forms!frmInput.txtLastName "
Random Solutions  
 
programming4us programming4us