Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropdown list - default value

Tags:

vba

ms-access

I have a dropdown list of all available printers:

Private Sub Form_Load()
    For Each l_pr In Application.Printers

      Me.dropdown.RowSourceType = "Value List"
      Me.dropdown.AddItem l_pr.DeviceName
   Next

   Me.dropdown.DefaultValue = Application.Printer.DeviceName

End Sub

I'd like to have the default printer to be selected when the form is loaded. I thought you could do that with DefaultValue (see my code), but nothing is displayed this way.

How can I achieve this?

like image 718
Bv202 Avatar asked May 12 '26 14:05

Bv202


1 Answers

Make your printer selection by assigning to the dropdown's Value property instead of DefaultValue.

I verified this code with a new form in Access 2007 ...

Private Sub Form_Load()
    Dim l_pr As Printer
    Me.dropdown.RowSourceType = "Value List"
    For Each l_pr In Application.Printers
        Me.dropdown.AddItem l_pr.DeviceName
    Next
    'Me.dropdown.DefaultValue = Application.Printer.DeviceName
    Me.dropdown.Value = Application.Printer.DeviceName
    Set l_pr = Nothing
End Sub

I moved the RowSourceType statement before the For loop ... you only need to run that statement once.

Also notice Dim l_pr As Printer ... declaring variables is recommended practice in VBA. Include Option Explicit in the Declarations section of your form module and then run Debug->Compile from the VB Editor's main menu. Fix anything else the compiler complains about before troubleshooting other code problems.

like image 128
HansUp Avatar answered May 14 '26 17:05

HansUp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!