Excel userform edit database table

Don Quixote

New Member
Joined
Feb 2, 2025
Messages
6
Office Version
  1. 2021
Platform
  1. Windows
I have an excel userform where I can search for and create new database entries in an ms access table.

But when I try to edit existing entries it gives me an error that the object doesn't support this action.

here is my code so far:
VBA Code:
Private Sub EditButton_Click()

    Dim con As Object
    Dim rs As Object

    Set con = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")
    
    con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Dennis\Documents\Blending & Filling\Basis Olie Lossing\Base Oils.accdb;"
    'Open Db connection
    con.Open
    Set rs.ActiveConnection = con
    
    rs.Open "Select * from [Planning] where [Bestelbon] = " & UserForm1.txtBestelbon.Text & ""
    'rs.Open "[Planning]", con, 1, 3, 2
    
    'rs.CursorLocation = adUseClient
    'rs.CursorType = adOpenStatic
    'rs.LockType = adLockBatchOptimistic
    
    rs.Edit
    rs.Fields("Productnaam") = UserForm1.txtProduct.Value
    rs.Fields("Transporteur") = UserForm1.txtTransporteur.Value
    'rs!Productnaam = UserForm1.txtProduct.Value
    'rs!Transporteur = UserForm1.txtTransporteur.Value
    rs.Update
    rs.Close
    
    Set rs = Nothing
    con.Close
    Set con = Nothing

End Sub

I would use the sql query to find a matching value of the one in txtBestelnummer textbox, and then edit the other fields in that row.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hello @Don Quixote.
Try next code:
VBA Code:
Private Sub CommandButton1_Click()
    Dim dbPath As String, SQL As String
    Dim cnn         As New ADODB.Connection
    Dim rst         As New ADODB.Recordset
    dbPath = "C:\Users\Mike\Documents\DB_Copy.accdb"   ' Replace with your path to the .accdb file

    cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath

    If Me.TextBox0.Value <> "" Then
        SQL = "SELECT * FROM Planning WHERE Id = " & Me.TextBox0.Value
    Else
        SQL = "SELECT * FROM Planning Where Id = 0"
    End If

    rst.Open SQL, cnn, adOpenKeyset, adLockOptimistic

    If rst.RecordCount = 0 Then
        rst.AddNew
    End If

    rst.Fields("Productnaam").Value = Me.TextBox1.Value
    rst.Fields("Transporteur").Value = Me.TextBox2.Value

    rst.Update

    MsgBox "Updated Successfully", vbInformation
    Call UserForm_Initialize
End Sub
Excel userform edit database table.png
Excel userform edit database table_v2.png

I hope I helped you this time too. Good luck.
 
Upvote 0
Solution

Forum statistics

Threads
1,226,214
Messages
6,189,671
Members
453,562
Latest member
overmyhead1

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