Fill userform textboxes with data from Access database

johnsonk

Board Regular
Joined
Feb 4, 2019
Messages
172
Hi, I have a userform with a combobox and some texboxes I can get data from access database for the combobox but I also want it to populate the textboxes with a combobox change event how do I do that ?

VBA Code:
Private Sub Userform_Initialize()
On Error GoTo UserForm_Initialize_Err
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=myDatabase.mdb"
rst.Open "SELECT DISTINCT [Product Code] FROM Stores Code ORDER BY [Product Code];", _
cnn, adOpenStatic
rst.MoveFirst
With Me.ComboBox1
.Clear
Do
.AddItem rst![Product Code]
rst.MoveNext
Loop Until rst.EOF
End With
UserForm_Initialize_Exit:
On Error Resume Next
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
Exit Sub
UserForm_Initialize_Err:
MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
Resume UserForm_Initialize_Exit
End Sub

Private Sub ComboBox1_Change()

??????

 End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
when user changes combobox1 , it fills the form

Code:
Private Sub ComboBox1_AfterUpdate()
LoadData2Form
End Sub


Private Sub LoadData2Form()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=myDatabase.mdb"
rst.Open "SELECT * from table where [Product Code] ='" & ComboBox1 & "';", cnn, adOpenStatic

With rst
  txtBox1 = .Fields("Name").Value & ""
  txtBox2 = .Fields("Addr").Value & ""
  txtBox3 = .Fields("Phone").Value & ""
Wend
Set rst = Nothing
End Sub
 
Upvote 0
Hi ranman256, Thanks for the code I am getting syntax error on the code below.

1595314697787.png


rst.Open "SELECT * from table where [Product Code] ='" & ComboBox1 & "';", cnn, adOpenStatic

Regards
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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