Link to home
Start Free TrialLog in
Avatar of lp3535
lp3535

asked on

Click a command button from other procedure

I writing a loop to load a userform if certain criteria are met, but I want to be able to just print the form and close it, then move onto the next item in the loop.

The problem is that the userform waits for me to click on either "ok" or "cancel" before it moves onto the next step.  My code is below.

        Dim myFirst As Object
        Set myFirst = ActiveDocument.CustomDocumentProperties
         
        Dim CurrentItem As ListItem
        Dim CurrentSubItem As ListView
   
        For Each CurrentItem In StockList.ListView1.ListItems
         
        Dim dToday, DHolding As Date
        Dim ddiff As Long
       
        DHolding = CurrentItem.SubItems(2)
        dToday = Date
       
        ddiff = DateDiff("d", dToday, DHolding)
             
        If ddiff < 3 Then
        myFirst.item("xListItem").Value = CurrentItem.Index
        CurrentItem.Bold = True
        ViewStock.Show
       ViewStock.Print
        Unload ViewStock
        Else
        CurrentItem.Bold = False
        End If
        Next
Avatar of Wikkard
Wikkard
Flag of Australia image

You can call an OK click event procedure directly from your main loop.
ie
ButtonOK_Click()
Avatar of lp3535
lp3535

ASKER

Hi Wikkard,

If I do that I get a compile error -  "=" expected.

take off the brackets or put the word call in front
ie ButtonOK_Click
or Call ButtonOK_Click()
When you put parenthesis it expects a return value from a function call.
Avatar of lp3535

ASKER

Hi Wikkard,

That doesn't work either.  I get "Sub or Function not defined".

ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

YourFormName.ButtonOK_Click
You need to call the function (on ViewStock form) that handles the OK button

ie ViewStock.btnOK_Click()
or you can just close the for directly after you print it

ie.
ViewStock.PrintForm
unload ViewStock
oops without the parens
ViewStock.btnOK_Click
Avatar of lp3535

ASKER

As usual GrahamSkan, your answer was spot on.

Thanks everyone else for your help.