Error Referencing Userform Object in A Standard Module

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am getting an "Object variable or With block variable not set" error with the line highlighted in red below. 'frmservice' refers to userform "frm_tservices", and is declared publically as an object at the start of my project. It's always been a challenge for me understanding how to refer to forms in standard modules and I think this proves the obvious.

Rich (BB code):
Sub iupdate_srvcufrm(ByRef frmservice As Object)
    Application.ScreenUpdating = False

    srvcs_no = Application.WorksheetFunction.CountA(ws_thold.Range("AK1:AK8"))
'Stop 'try save      SAVE OK
    If srvcs_no = 1 Then
        frmservice.Controls("btn_help").Visible = False
        frmservice.Controls("cbt_s1_del").Visible = True
        frmservice.Controls("cbt_s1_del").Enabled = True
        frmservice.Controls("cbt_s1_end").Enabled = True
        frmservice.Controls("cbt_s1_add").Enabled = True
        If ojs = True Then
            frmservice.Controls("frm_service1").Caption = "  SERVICE 1**  "
        Else
            frmservice.Controls("frm_service1").Caption = "  SERVICE 1*  "
        End If
    End If
    .....

This above procedure is called (purple line) from this procedure ...

Rich (BB code):
Sub ichk_overlap(ByRef add_start As Double)
Dim frmservice As Object
'Set frmservice = Me  'this resulted in an invalid use of Me keyword

    With ws_thold
        .Visible = xlSheetVisible
        .Activate
       
        no_svcs = Application.WorksheetFunction.CountA(.Range("AJ1:AJ8"))
        If no_svcs = 1 Then Exit Sub 'nothing to compare to
        For samir = 1 To no_svcs
            txt_h_lwr = .Cells(samir, 37)
            txt_h_upr = .Cells(samir, 38)
            If Right(txt_h_lwr, 1) = "A" Then l_ap = "AM"
            If Right(txt_h_lwr, 1) = "P" Then l_ap = "PM"
            If Right(txt_h_upr, 1) = "A" Then u_ap = "AM"
            If Right(txt_h_upr, 1) = "P" Then u_ap = "PM"
            tm_h_lwr = Left(txt_h_lwr, Len(txt_h_lwr) - 1)
            tm_h_upr = Left(txt_h_upr, Len(txt_h_upr) - 1)
            str_h_lwr = tm_h_lwr & " " & l_ap
            str_h_upr = tm_h_upr & " " & u_ap
       
            h_lwr = CDate(str_h_lwr)
            h_upr = CDate(str_h_upr)
            'Stop
            If add_start >= h_lwr And add_start <= h_upr And .Cells(samir, 37).Font.Color = vbBlack Then
                MsgBox "There is already a service scheduled for tis time period.", vbExclamation, "ERROR: Overlapping Service"
                For fgrnln = 1 To 8
                    If .Cells(fgrnln, 37).Font.Color = vbGreen Then
                        .Range("AJ" & fgrnln & ":AQ" & fgrnln).Delete Shift:=xlUp
                        iupdate_srvcufrm frmservice
                        Exit For
                    End If
                Next fgrnln
                Exit Sub
            End If
        Next samir
    End With
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi Mike. It's already loaded and active. These procedures are parts of the forms buttons clicks.
 
Upvote 0
In the sub ichk_overlap, the variable frmservice is declared in the Dim statement, but no object is assigned to that variable, i.e. it is Nothing.
The line iupdate_srvcufrm frmservice passes Nothing as the argument to iupdata_srvcufrm
And the error occurs.
 
Upvote 0
Thanks Mike ... I need to set the object (userform) frmservice in sub ichk_overlap?
Code:
 set frmservice = Userform("frm_tservices")
??
 
Upvote 0
Yes, although the collection is Userforms rather than your singular form.
Or, if its the only userform that is loaded
VBA Code:
Set frmservice = Userforms(0)
 
Upvote 0

Forum statistics

Threads
1,215,067
Messages
6,122,949
Members
449,095
Latest member
nmaske

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