Userform unable to use

Mike_CS

New Member
Joined
Jun 21, 2017
Messages
24
Hi

Can anybody put me right on a 'Compile error: Sub or Function not defined' please.

Have had my first try at a userform and have copied this from the web.

The userform has many labels and text boxes and a few combo boxes. Basically i'm looking for the form to populate a table called "datastore" which is on the "datastore" tab.

i get the above error message which also highlights the phrase "offset" on row 8. Can anybody give me a clue as i've drawn a complete blank (the code im using is below).

Apologies if this has been answered a hundred times. i cannot find anything that works.
Code:
Private Sub CommandButton1_Click()

    'Copy input values to sheet.
    Dim oNewRow As ListRow
    Dim rng As Range


    
    Set rng = ThisWorkbook.Worksheets("Datastore").Range("datastore")
    rng.Select
    Set oNewRow = Selection.ListObject.ListRows.Add(AlwaysInsert:=True)
    With ws
        oNewRow.Range.Cells(1, 1).value = Offset(0, -1).value + 1
        oNewRow.Range.Cells(1, 2).value = Me.Label2.value
        oNewRow.Range.Cells(1, 3).value = Me.Label13.value
        oNewRow.Range.Cells(1, 4).value = Me.TextBox1.value
        oNewRow.Range.Cells(1, 5).value = Me.Label18.value
        oNewRow.Range.Cells(1, 6).value = Me.Label15.value
        oNewRow.Range.Cells(1, 8).value = Me.ComboBox3.value
        oNewRow.Range.Cells(1, 9).value = Me.TextBox2.value
        oNewRow.Range.Cells(1, 10).value = Me.ComboBox2.value
        oNewRow.Range.Cells(1, 11).value = Me.Label16.value
        oNewRow.Range.Cells(1, 12).value = Me.Label19.value
        oNewRow.Range.Cells(1, 13).value = Me.Label15.value
        oNewRow.Range.Cells(1, 18).value = Me.Label15.value
        oNewRow.Range.Cells(1, 16).value = Me.Label2.value
        oNewRow.Range.Cells(1, 17).value = Me.Label13.value


    End With


'    Clear input controls.
'    Clear_create_form
    
End Sub
 
Last edited by a moderator:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
You need to offset from a range. rng.Offset or Selection.Offset for example.

Steven. I love you and want to have your babies ha
it has now moved on to the next line but i am now getting a "compile error: method or data member not found" and highlighting the .value part now. I have changed the code a little
Code:
Private Sub cmdSubmit_Click()
    
    'This button will be used to add new entry to database and email details to person allocated


    'Copy input values to sheet.
    Dim oNewRow As ListRow
    Dim rng As Range


    
    Set rng = ThisWorkbook.Worksheets("Datastore").Range("datastore")
    rng.Select
    
    Set oNewRow = Selection.ListObject.ListRows.Add(AlwaysInsert:=True)
    With ws
        oNewRow.Range.Cells(1, 1).value = rng.Offset(0, -1).value + 1
        oNewRow.Range.Cells(1, 2).value = Me.lblAlloc8rFullnameCR.value
        oNewRow.Range.Cells(1, 3).value = Me.lblAlloc8rPIDCR.value
        oNewRow.Range.Cells(1, 4).value = Me.txtRequestor.value
        oNewRow.Range.Cells(1, 5).value = Me.lblDateRec.value
        oNewRow.Range.Cells(1, 6).value = Me.lblTodayCR.value
        oNewRow.Range.Cells(1, 8).value = Me.cmbReqType.value
        oNewRow.Range.Cells(1, 9).value = Me.txtReqDetails.value
        oNewRow.Range.Cells(1, 10).value = Me.cmbAlloc8d2Name.value
        oNewRow.Range.Cells(1, 11).value = Me.lblAlloc8d2PID.value
        oNewRow.Range.Cells(1, 12).value = Me.lblToBDoneBy.value
        oNewRow.Range.Cells(1, 13).value = Me.lblTodayCR.value
        oNewRow.Range.Cells(1, 18).value = Me.lblTodayCR.value
        oNewRow.Range.Cells(1, 16).value = Me.lblAlloc8rFullnameCR.value
        oNewRow.Range.Cells(1, 17).value = Me.lblAlloc8rPIDCR.value


    End With


'    Clear input controls.
'    Clear_create_form
    
End Sub
Is this a pile of rubbish what i have written?
 
Last edited by a moderator:
Upvote 0
This now seems to work for a self contained dataset with userform
Code:
Sub Insert_record()

    'Copy input values to sheet.

    Dim lblAlloc8rFullnameCR As Label
    Dim lblAlloc8rPIDCR As Label
    Dim txtRequestor As Textbox
    Dim lblDateRec As Label
    Dim lblTodayCR As Label
    Dim cmbReqType As Combobox
    Dim txtReqDetails As Textbox
    Dim cmbAlloc8d2Name As Combobox
    Dim lblAlloc8d2PID As Label
    Dim lblToBDoneBy As Label
    Dim the_sheet As Worksheet
    Dim table_list_object As ListObject
    Dim table_object_row As ListRow

    Set the_sheet = Sheets("datastore")
    Set table_list_object = the_sheet.ListObjects(1)
    Set table_object_row = table_list_object.ListRows.Add

        table_object_row.Range.Cells(1, 2).Value = FrmCreateRequest.lblAlloc8rFullnameCR
        table_object_row.Range.Cells(1, 3).Value = FrmCreateRequest.lblAlloc8rPIDCR
        table_object_row.Range.Cells(1, 4).Value = FrmCreateRequest.txtRequestor
        table_object_row.Range.Cells(1, 5).Value = FrmCreateRequest.lblDateRec
        table_object_row.Range.Cells(1, 6).Value = FrmCreateRequest.lblTodayCR
        table_object_row.Range.Cells(1, 8).Value = FrmCreateRequest.cmbReqType
        table_object_row.Range.Cells(1, 9).Value = FrmCreateRequest.txtReqDetails
        table_object_row.Range.Cells(1, 10).Value = FrmCreateRequest.cmbAlloc8d2Name
        table_object_row.Range.Cells(1, 11).Value = FrmCreateRequest.lblAlloc8d2PID
        table_object_row.Range.Cells(1, 12).Value = FrmCreateRequest.lblToBDoneBy
        table_object_row.Range.Cells(1, 13).Value = FrmCreateRequest.lblTodayCR
        table_object_row.Range.Cells(1, 18).Value = FrmCreateRequest.lblTodayCR
        table_object_row.Range.Cells(1, 16).Value = FrmCreateRequest.lblAlloc8rFullnameCR
        table_object_row.Range.Cells(1, 17).Value = FrmCreateRequest.lblAlloc8rPIDCR

End Sub
Also worked out how to get it to write to an external workbook table.
Code:
Sub Insert_record2Standalone()
   
    'Copy input values to sheet.
    
    Dim lblAlloc8rFullnameCR As Label
    Dim lblAlloc8rPIDCR As Label
    Dim txtRequestor As Textbox
    Dim lblDateRec As Label
    Dim lblTodayCR As Label
    Dim cmbReqType As Combobox
    Dim txtReqDetails As Textbox
    Dim cmbAlloc8d2Name As Combobox
    Dim lblAlloc8d2PID As Label
    Dim lblToBDoneBy As Label
    Dim the_book As Workbook
    Dim the_sheet As Worksheet
    Dim table_list_object As ListObject
    Dim table_object_row As ListRow
    
Application.ScreenUpdating = False
    
    Set the_book = Workbooks.Open(Filename:="your full filepath\name and extension")
    Set the_sheet = Sheets("name of tab with table")
    Set table_list_object = the_sheet.ListObjects(1)
    Set table_object_row = table_list_object.ListRows.Add
    
        table_object_row.Range.Cells(1, 2).Value = FrmCreateRequest.lblAlloc8rFullnameCR
        table_object_row.Range.Cells(1, 3).Value = FrmCreateRequest.lblAlloc8rPIDCR
        table_object_row.Range.Cells(1, 4).Value = FrmCreateRequest.txtRequestor
        table_object_row.Range.Cells(1, 5).Value = FrmCreateRequest.lblDateRec
        table_object_row.Range.Cells(1, 6).Value = FrmCreateRequest.lblTodayCR
        table_object_row.Range.Cells(1, 8).Value = FrmCreateRequest.cmbReqType
        table_object_row.Range.Cells(1, 9).Value = FrmCreateRequest.txtReqDetails
        table_object_row.Range.Cells(1, 10).Value = FrmCreateRequest.cmbAlloc8d2Name
        table_object_row.Range.Cells(1, 11).Value = FrmCreateRequest.lblAlloc8d2PID
        table_object_row.Range.Cells(1, 12).Value = FrmCreateRequest.lblToBDoneBy
        table_object_row.Range.Cells(1, 13).Value = FrmCreateRequest.lblTodayCR
        table_object_row.Range.Cells(1, 18).Value = FrmCreateRequest.lblTodayCR
        table_object_row.Range.Cells(1, 16).Value = FrmCreateRequest.lblAlloc8rFullnameCR
        table_object_row.Range.Cells(1, 17).Value = FrmCreateRequest.lblAlloc8rPIDCR
        
    the_book.Save
    the_book.Close
    
    MsgBox "Information Sucessfully Updated"


End Sub

Thanks for making me think a little different. got there in the end.
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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