Results 1 to 3 of 3
  1. #1
    ngruson is offline Novice
    Windows XP Access 2003
    Join Date
    Apr 2010
    Posts
    6

    Control variable is Null

    Hi all,



    I have a form in Access with a listbox on it. The forms works as expected. But now I want to assign this listbox to a variable in a piece of new code.

    Code:
    Dim ctrl As Control
    Set ctrl = Me![MyListBox]
    MyListBox is exactly the name of the listbox. But when I evaluate the variable ctrl after this piece of code executes, the value of the variable is Null.
    Option Explicit is set on this form. What am I doing wrong here?

    Thanks in advance,
    Nils Gruson

  2. #2
    SoftwareMatters is offline Access VBA Developers
    Windows XP Access 2003
    Join Date
    Mar 2009
    Location
    Dorset
    Posts
    274
    What are you trying to assign - all the contents in the listbox or just the selected value?

  3. #3
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    The value will continue to be null until and unless you select a value on the List box.
    Now it is not very clear what you actually want to do. but I assume that you want to pass the value selected on the List box on to a variable here clr and use it.

    This is what I have done:

    Private Sub Form_Current()
    Me.List0.SetFocus
    Me.List0.ListIndex = 0
    End Sub

    I put the above code on the onCurrent Event of my form. So when my form loads the Focus is set to List0 and the first Item is selected.

    Now this Code is attached to a command button in the form that I have used to display the value of variable clr. It is not Null any more as the First Item is already selected when the form Opened.


    Private Sub Command2_Click()
    On Error GoTo Err_Command2_Click
    Dim clr As Control
    Set clr = Me!
    [List0]
    MsgBox clr

    Exit_Command2_Click:
    Exit Sub

    Err_Command2_Click:
    MsgBox Err.Description
    Resume Exit_Command2_Click

    End Sub

    hope this clears out the issue.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Using a string variable to specify a control
    By Gerry in forum Programming
    Replies: 3
    Last Post: 04-14-2010, 02:28 PM
  2. using forms control as report variable
    By Seven in forum Reports
    Replies: 3
    Last Post: 12-19-2009, 04:04 PM
  3. Null Date control code problem
    By DanW in forum Forms
    Replies: 2
    Last Post: 11-10-2009, 03:13 PM
  4. List box value to variable - invalid use of null?
    By Orabidoo in forum Programming
    Replies: 2
    Last Post: 05-07-2009, 11:06 AM
  5. Sending control to specific control
    By wasim_sono in forum Programming
    Replies: 2
    Last Post: 04-19-2007, 08:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums