userform to SQL mass UPDATE statment

austin350s10

Active Member
Joined
Jul 30, 2010
Messages
321
So I am working on a userform that populates all of its fields from a database (works flawlessly). The problem comes in when I attempt to UPDATE the database with any changes to the userform fields. To make matching easy, each userform field name is also a column name in the database. So i figured i could just run through each control on the userform and assign its value to the database when updating. My idea works but, it takes forever as there are over 300 controls on my userform. Does anyone know of a more efficient way to do this?

I already tried grouping multiple SQL strings together and running one execute command but, being that there are some extra controls on my userform that are not in my database, I keep getting errors.

My slow but working attempt:
Code:
Private Sub btnSaveChanges_Click()
Dim ctl As MSForms.Control

Set Sql = SQLconnect("myserver login")

For Each ctl In Me.Controls
    On Error Resume Next
    If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "CheckBox" Or TypeName(ctl) = "ComboBox" Then
        sqlstr = "UPDATE Clients SET " & ctl.Name & " = '" & ctl.Value & "' WHERE idClients = '1';"
        Sql.Execute sqlstr
    End If
Next ctl

End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,214,613
Messages
6,120,515
Members
448,968
Latest member
Ajax40

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