Trying to imitate gets & puts in VBA

LMacD

New Member
Joined
May 18, 2019
Messages
28
I have a number of forms that get data from tables or add data to tables. Each form needs its own subroutine to do that.

The column names and the corresponding form fields all use the same name. I pass the form name and the table row to the Get or Place sub.

Now when I change my table I have only one place to update the code.

This works but I have a clunky way of finding the user form name.

I can pass the form name to the sub. Is there a better way to do this.

Not every form uses every table column, so I just skip it if I do not need it.


VBA Code:
Public Sub PutCustomer(fName As String, intRow As Integer)
    Dim Obj As Object
    For Each Obj In VBA.UserForms
        If StrComp(Obj.Name, fName, vbTextCompare) = 0 Then
            GoTo SetCustomer
        End If
    Next Obj
    Exit Sub
    
SetCustomer:
    On Error Resume Next
    With lob_tblCustomer
        .ListColumns("CustNum").DataBodyRange(intRow) = Val(Obj.CustNum)
        .ListColumns("CustStatus").DataBodyRange(intRow) = Obj.CustStatus
        .ListColumns("CustName").DataBodyRange(intRow) = Obj.CustName
        .ListColumns("CustSpouse").DataBodyRange(intRow) = Obj.CustSpouse
        .ListColumns("CustAdd1").DataBodyRange(intRow) = Obj.CustAdd1
        .ListColumns("CustAdd2").DataBodyRange(intRow) = Obj.CustAdd2
        .ListColumns("CustApt").DataBodyRange(intRow) = Obj.CustApt
        .ListColumns("CustCity").DataBodyRange(intRow) = Obj.CustCity
        .ListColumns("CustState").DataBodyRange(intRow) = Obj.CustState
        .ListColumns("CustZIP").DataBodyRange(intRow) = Obj.CustZIP
        .ListColumns("CustPrimePh").DataBodyRange(intRow) = Obj.CustPrimePh
        .ListColumns("CustAltPh").DataBodyRange(intRow) = Obj.CustAltPh
        .ListColumns("CustEmail").DataBodyRange(intRow) = Obj.CustEmail
        .ListColumns("CustLookUp").DataBodyRange(intRow) = Obj.CustPrimePh
    End With
    On Error GoTo 0
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Why not do something like this (untested):

VBA Code:
Public Sub PutCustomer(Obj as Object, intRow As Integer)
    On Error Resume Next
    With lob_tblCustomer
        .ListColumns("CustNum").DataBodyRange(intRow) = Val(Obj.CustNum)
        .ListColumns("CustStatus").DataBodyRange(intRow) = Obj.CustStatus
        .ListColumns("CustName").DataBodyRange(intRow) = Obj.CustName
        .ListColumns("CustSpouse").DataBodyRange(intRow) = Obj.CustSpouse
        .ListColumns("CustAdd1").DataBodyRange(intRow) = Obj.CustAdd1
        .ListColumns("CustAdd2").DataBodyRange(intRow) = Obj.CustAdd2
        .ListColumns("CustApt").DataBodyRange(intRow) = Obj.CustApt
        .ListColumns("CustCity").DataBodyRange(intRow) = Obj.CustCity
        .ListColumns("CustState").DataBodyRange(intRow) = Obj.CustState
        .ListColumns("CustZIP").DataBodyRange(intRow) = Obj.CustZIP
        .ListColumns("CustPrimePh").DataBodyRange(intRow) = Obj.CustPrimePh
        .ListColumns("CustAltPh").DataBodyRange(intRow) = Obj.CustAltPh
        .ListColumns("CustEmail").DataBodyRange(intRow) = Obj.CustEmail
        .ListColumns("CustLookUp").DataBodyRange(intRow) = Obj.CustPrimePh
    End With
    On Error GoTo 0
End Sub
In the calling form you would have something like this:
VBA Code:
Private Sub cmbSave_Click()
    PutCustomer Me, cInt(Me.CustNum.Value)
End Sub
 
Upvote 0
Solution
I have a number of forms that get data from tables or add data to tables. Each form needs its own subroutine to do that.

The column names and the corresponding form fields all use the same name. I pass the form name and the table row to the Get or Place sub.

Now when I change my table I have only one place to update the code.

This works but I have a clunky way of finding the user form name.

I can pass the form name to the sub. Is there a better way to do this.

Not every form uses every table column, so I just skip it if I do not need it.


VBA Code:
Public Sub PutCustomer(fName As String, intRow As Integer)
    Dim Obj As Object
    For Each Obj In VBA.UserForms
        If StrComp(Obj.Name, fName, vbTextCompare) = 0 Then
            GoTo SetCustomer
        End If
    Next Obj
    Exit Sub
  
SetCustomer:
    On Error Resume Next
    With lob_tblCustomer
        .ListColumns("CustNum").DataBodyRange(intRow) = Val(Obj.CustNum)
        .ListColumns("CustStatus").DataBodyRange(intRow) = Obj.CustStatus
        .ListColumns("CustName").DataBodyRange(intRow) = Obj.CustName
        .ListColumns("CustSpouse").DataBodyRange(intRow) = Obj.CustSpouse
        .ListColumns("CustAdd1").DataBodyRange(intRow) = Obj.CustAdd1
        .ListColumns("CustAdd2").DataBodyRange(intRow) = Obj.CustAdd2
        .ListColumns("CustApt").DataBodyRange(intRow) = Obj.CustApt
        .ListColumns("CustCity").DataBodyRange(intRow) = Obj.CustCity
        .ListColumns("CustState").DataBodyRange(intRow) = Obj.CustState
        .ListColumns("CustZIP").DataBodyRange(intRow) = Obj.CustZIP
        .ListColumns("CustPrimePh").DataBodyRange(intRow) = Obj.CustPrimePh
        .ListColumns("CustAltPh").DataBodyRange(intRow) = Obj.CustAltPh
        .ListColumns("CustEmail").DataBodyRange(intRow) = Obj.CustEmail
        .ListColumns("CustLookUp").DataBodyRange(intRow) = Obj.CustPrimePh
    End With
    On Error GoTo 0
End Sub

Why not do something like this (untested):

VBA Code:
Public Sub PutCustomer(Obj as Object, intRow As Integer)
    On Error Resume Next
    With lob_tblCustomer
        .ListColumns("CustNum").DataBodyRange(intRow) = Val(Obj.CustNum)
        .ListColumns("CustStatus").DataBodyRange(intRow) = Obj.CustStatus
        .ListColumns("CustName").DataBodyRange(intRow) = Obj.CustName
        .ListColumns("CustSpouse").DataBodyRange(intRow) = Obj.CustSpouse
        .ListColumns("CustAdd1").DataBodyRange(intRow) = Obj.CustAdd1
        .ListColumns("CustAdd2").DataBodyRange(intRow) = Obj.CustAdd2
        .ListColumns("CustApt").DataBodyRange(intRow) = Obj.CustApt
        .ListColumns("CustCity").DataBodyRange(intRow) = Obj.CustCity
        .ListColumns("CustState").DataBodyRange(intRow) = Obj.CustState
        .ListColumns("CustZIP").DataBodyRange(intRow) = Obj.CustZIP
        .ListColumns("CustPrimePh").DataBodyRange(intRow) = Obj.CustPrimePh
        .ListColumns("CustAltPh").DataBodyRange(intRow) = Obj.CustAltPh
        .ListColumns("CustEmail").DataBodyRange(intRow) = Obj.CustEmail
        .ListColumns("CustLookUp").DataBodyRange(intRow) = Obj.CustPrimePh
    End With
    On Error GoTo 0
End Sub
In the calling form you would have something like this:
VBA Code:
Private Sub cmbSave_Click()
    PutCustomer Me, cInt(Me.CustNum.Value)
End Sub
Thanks Jan, that worked great and is much cleaner. The only minor change I made in the table row is a variable not a formfield.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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