VBA to Loop through Combo_box?

Endlessknight

New Member
Joined
Oct 28, 2016
Messages
36
Greetings all,

I inherited a databases from a co-worker. It has a form with a combo_box and button to generate a report based on the item selected via the combo_box.

I wrote a function that, see below, that opens the report, looks up the location to save it in a table, saves it a then closes the report.

How can I get the function to loop through all the items in the combo_box without me having to manually select them?

Thanks in advance for any input, it's greatly appreciated.


Code:
Function Expo_allPDF()


Dim MyPath As String


MyPath = DLookup("Field2", "Table", "Field1 = '" & [Forms]![ChgsMstrCommands]![Combo_SelectRVURenderingForRpts] & "'")


DoCmd.OpenReport "Reportname", 2
DoCmd.OutputTo 3, "Reportname", acFormatPDF, MyPath, True
DoCmd.Close 3, "Reportname"


End Function
 
I know that item data for a list box must be a variant (99.7% sure of that). You're using a combo? I'd go with variant since logic would dictate the designers would follow a pattern. Plus, it would work with strings, numbers, empty strings or Nulls. You could try it both ways just for fun (at least I would) but I would use variant regardless for the reasons given.
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
For i = 0 To ListControl.ListCount - 1
itm = ListControl.ItemData(i)
MyPath = DLookup("Location", "Provider RVU & ATO Reports", "[Rendering] = '" & itm & "'")
DoCmd.OpenReport "ProviderRVUHandoutSingle", 2
DoCmd.OutputTo 3, "ProviderRVUHandoutSingle", acFormatPDF, MyPath, False
DoCmd.Close 3, "ProviderRVUHandoutSingle"
Next

Without the itm variable is simple enough:
Code:
For i = 0 To ListControl.ListCount - 1
  MyPath = DLookup("Location", "Provider RVU & ATO Reports", "[Rendering] = '" & ListControl.ItemData(i)  & "'")
  DoCmd.OpenReport "ProviderRVUHandoutSingle", 2
  DoCmd.OutputTo 3, "ProviderRVUHandoutSingle", acFormatPDF, MyPath, False
  DoCmd.Close 3, "ProviderRVUHandoutSingle"
Next
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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