Excel userform and MS Access

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
641
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I'm trying to populate a userform based on an employee number, but I am not at that point yet. Right now, I'm just trying to get their first name based on their number. Unfortunately, my code doesn't work. As soon as I start typing in their employee number, the following line of code appears in the "frmPersonnelEntry.txtFN" textbox:
VBA Code:
SELECT [First Name] FROM Personnel WHERE [Employee #] =
but after the equal sign is the value in frmPersonnelEntry.txtEmpNum.Text.

So basically, what I am wanting, as the user types in the employee number, vba keeps looking into the database until the record is found.

Example:
Employee #: 12345
Employee First Name: John

ENTRY:
frmPersonnelEntry.txtEmpNum.Text = 1 'NOTHING FOUND
frmPersonnelEntry.txtFN.Text =

frmPersonnelEntry.txtEmpNum.Text = 12 '1 is already there NOTHING FOUND
frmPersonnelEntry.txtFN.Text =

frmPersonnelEntry.txtEmpNum.Text = 123 '12 is already there NOTHING FOUND
frmPersonnelEntry.txtFN.Text =

frmPersonnelEntry.txtEmpNum.Text =1234 '123 is already there NOTHING FOUND
frmPersonnelEntry.txtFN.Text =

frmPersonnelEntry.txtEmpNum.Text = 12345 '1234 is already there RECORD FOUND
frmPersonnelEntry.txtFN.Text = John

I hope my explanation of what I'm wanting to achieve makes sense. I'm familiar with using MS Access syntax within Excel VBA. Thank you.

VBA Code:
Private Sub txtEmpNum_Change()
    Call FindRecordInAccess
End Sub

VBA Code:
Sub FindRecordInAccess()

    'Path
    Dim strPath As String

    'Provider
    Dim strProv As String

    'Connection String
    Dim strConn As String

    'Connection
    Dim Conn As New Connection

    'RecordSet
    Dim reQry As New Recordset

    'SQL Query
    Dim strQry As String

    strPath = "C:\Users\123456\OneDrive\Documents\Personnel.accdb"
    strProv = "Microsoft.ACE.OLEDB.12.0;"
    strConn = "Provider=" & strProv & "Data Source=" & strPath

    'Connection Open
    Conn.Open strConn

    strQry = "SELECT [First Name] FROM Personnel WHERE [Employee #] =" & frmPersonnelEntry.txtEmpNum.Text
    Debug.Print strQry
   
    frmPersonnelEntry.txtFN = strQry

'More code later

End Sub
 

Attachments

  • Screenshot 2025-03-05 033833.jpg
    Screenshot 2025-03-05 033833.jpg
    11.6 KB · Views: 3
You haven't done anything about actually executing the query. ;) All your code does is to put the query text into the textbox. You need to set and use your recordset - something like:

Code:
set reQry = conn.execute strQry
frmPersonnelEntry.txtFN = reQry(0)
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top