0

I have a database in Access and I want to enter some data from Excel VBA form into the database and also want to retrieve some data back into the form.

Sceenshot of the procedure is

This is the Add Button Code :

Private Sub CommandButton1_Click()

Dim con As New ADODB.Connection
Dim connectionstring As String

Dim sql As String
connectionstring = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
connectionstring = connectionstring & "DATA Source=C:\Simple.accdb;"
con.Open connectionstring

sql = "insert into T1(ID,FName,Email)values('" & TextBox1.Text & "', '" & TextBox2.Text & "','" & TextBox3.Text & "')"


con.Execute sql


MsgBox "Values Entered", vbInformation

con.Close
End Sub

Here is the Search Button Code

Private Sub CommandButton3_Click()
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
con.connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Simple.accdb;"
'Open Db connection
con.Open
Set rs.ActiveConnection = con
rs.Open "Select * from T1 where ID= '" & UserForm1.TextBox1.Text & "'"
StartRow = 3
Do Until rs.EOF

'FName
UserForm1.TextBox2.Text = rs.Fields(1).Value
'Email
UserForm1.TextBox3.Text = rs.Fields(2).Value

rs.MoveNext
StartRow = StartRow + 1
Loop
Set rs = Nothing
con.Close
Set con = Nothing
End Sub

My problem is though I am able to add through the VBA form with ADD button, my search button code is not working. It says datatype mismatch in criteria expression.

All I wanted to do is perform a search on the basis of ID.

Please Help.

2
  • Please note if I change the sql query to rs.Open "Select * from T1 " in the Search option, it is showing the last record in the table Commented Jun 28, 2015 at 21:31
  • See this answer to a related question Commented Jun 28, 2015 at 21:47

1 Answer 1

2

I think the ID field in your table is numeric - you need to remove the single quotes from around the criteria in your sql statement in the search button code.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes did that and it already worked. Thanks though for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.