Results 1 to 7 of 7
  1. #1
    dreamnauta is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2011
    Posts
    16

    New to VBA - Textbox/ List question

    Hi everyone,

    I decided to begin learning access vba to make my databases more user friendly/interactive to non-access users. Here is a picture of the controls on my form: http://imageshack.us/photo/my-images/683/35819300.png/



    The idea is when a user enters a CIP number in the 'Add CIP' Field, an add will occur to the listbox as well.

    Here is my first function:

    Code:
     
    Private Sub listBoxPopulate()
    Dim strTextInput As String
    Dim textInput As Integer
     
    strTextInput = Text45.Text
     
    If strTextInput <> Null Then
    textInput = CInt(strTextInput)
    List43.Add (textInput)
     
    End If
    End Sub
    When I try to compile it says access is missing an object reference. Now to use the text property it says I have to give the box focus. How do I do this?

    - Josh

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    There are a few things to work on.

    For starters, the focus error is because you use the .Text property. You want the .Value property, which is the default. You can't test for Null that way. You could use the IsNull() function. That said, a String variable can never be Null (it could be a zero length string: ""). I might test with IsNumeric, since that appears to be your end goal.

    I personally rarely use Value List for list or combo boxes, I typically have a table that stores the options. That means I'd be adding a record to the underlying table and doing a Requery on the listbox. However, I think to add an item to a Value List the method is AddItem, not Add.

    Let's see where that gets us.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    dreamnauta is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2011
    Posts
    16
    Thanks Paul, you were very helpful!

  4. #4
    dreamnauta is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2011
    Posts
    16
    There seems to be a problem compiling due to "missing reference object".
    What am I doing wrong? Any help appreciated.

    P.S. I have programmed in a variety of languages, so i'm not completely new to programming. I read through Access 2010 VBA Macro Programming by Richard Shephard which was a good overview.

    Code:
     
    Private Sub listBoxPopulate()
    Dim strTextInput As String
    Dim textInput As Integer
     
    //error in this line
    strTextInput = Text45.Value 
     
    If strTextInput = IsNumeric(strTextInput) Then
    textInput = CInt(strTextInput)
    List43.AddItem (textInput)
    End If
    End Sub
     
    Private Sub Command52_Click()
    listBoxPopulate
    End Sub
    Last edited by dreamnauta; 12-20-2011 at 03:07 PM. Reason: clarification

  5. #5
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    That's an odd error, though I suspect I know what is causing it. The "Me" keyword is your friend, as it provides a shortcut to refer to whatever object the code is in (ie the form or report). It will give you Intellisense as soon as you type "Me.", which will help make sure you don't misspell object names. Try

    strTextInput = Me.Text45.Value

    which presumes Text45 is a textbox with a value in it (note that a String variable can't take a Null, so that will error if the textbox is Null). This test won't work:

    If strTextInput = IsNumeric(strTextInput) Then

    IsNumeric() will return a Boolean, ie either True or False (the value tested is either numeric or not). I'd use a test like:

    Code:
    If IsNumeric(strTextInput) Then
      'do what you want with a numeric input
    Else
      'warn the user the input wasn't valid
    End If
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    dreamnauta is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2011
    Posts
    16
    Thanks for all the help. My error was in referencing a 'outside' form.

    Here is the correct code for reference:

    Code:
     
    Public Sub listBoxPopulate()
        Dim strTextInput As String
        Dim textInput As Long
       
        strTextInput = Forms!Enrollment_GUI!inputTextBox.Value
        
        If IsNumeric(strTextInput) Then
            textInput = CLng(strTextInput)
            Forms!Enrollment_GUI!listBox.AddItem (textInput)
        Else
           MsgBox ("Please enter numeric data")
        End If
    End Sub
    Public Sub Command52_Click()
           listBoxPopulate
    End Sub
    Thanks again!

  7. #7
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Load textbox from another textbox
    By siddel77 in forum Programming
    Replies: 12
    Last Post: 08-30-2011, 01:46 PM
  2. Mailing list question
    By JoeyG54 in forum Access
    Replies: 3
    Last Post: 08-23-2011, 01:27 PM
  3. Replies: 4
    Last Post: 06-16-2011, 09:30 PM
  4. Combo/List box question
    By wacky1 in forum Forms
    Replies: 2
    Last Post: 05-22-2011, 09:07 PM
  5. Question about Value List
    By Buakaw in forum Access
    Replies: 3
    Last Post: 02-18-2011, 04:25 AM

Tags for this Thread

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