Bv202
Bv202

Reputation: 4044

Dropdown list - default value

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?

Upvotes: 3

Views: 1542

Answers (1)

HansUp
HansUp

Reputation: 97101

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.

Upvotes: 5

Related Questions