Compile error: Variable not defined
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Compile error: Variable not defined

  1. #1
    Join Date
    Jan 2012
    Posts
    14

    Compile error: Variable not defined

    Hi GUru's,

    Good day!

    I have a trouble with the Listview1 when i try to select the category it says "Variable not defined". i put all the necessary references and components. Perhaps i've overlooked some code that needs to be in the project. Can somebody check this for me please...

    Here's the code:



    Option Explicit

    Dim RS_CAT_SEL As New ADODB.Recordset

    Private Sub Command1_Click()
    If ListView1.ListItems.Count < 1 Then Unload Me: Exit Sub
    select_rec
    End Sub
    Sub select_rec()
    With rs_employee_ae ' this code highlights the error
    .Cat_Index = ListView1.SelectedItem.ListSubItems(1)
    .Text3.Text = ListView1.SelectedItem.ListSubItems(2)
    End With
    Unload Me
    End Sub

    Private Sub Command13_Click()
    Unload Me
    End Sub

    Private Sub Form_Load()
    RS_CAT_SEL.Open "SELECT * FROM tblCategory ORDER BY CategoryName ASC", CN, adOpenStatic, adLockReadOnly
    Load_Rec
    End Sub

    Private Sub Form_Unload(Cancel As Integer)
    Set RS_CAT_SEL = Nothing
    End Sub
    Sub Load_Rec()
    Screen.MousePointer = vbHourglass

    Call FillListView(ListView1, RS_CAT_SEL, 3, 1, True, True)

    Screen.MousePointer = vbDefault
    If ListView1.ListItems.Count < 1 Then Command1.Caption = "&Close"
    End Sub

    Private Sub ListView1_DblClick()
    select_rec
    End Sub

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,875

    Re: Compile error: Variable not defined

    Can't read it, plus a few basics:

    Use CODE TAGS so we can read your code.
    Code:
    ' Like this
    Point out the Error Line, and ERROR MESSAGE

    And, then, we might be able to help
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,784

    Re: Compile error: Variable not defined

    I have a trouble with the Listview1 when i try to select the category it says "Variable not defined".
    Code:
    Dim RS_CAT_SEL As New ADODB.Recordset
    
    Private Sub Command1_Click()
        If ListView1.ListItems.Count < 1 Then Unload Me: Exit Sub
        select_rec
    End Sub
    Sub select_rec()
        With rs_employee_ae ' this code highlights the error
            .Cat_Index = ListView1.SelectedItem.ListSubItems(1)
            .Text3.Text = ListView1.SelectedItem.ListSubItems(2)
        End With
        Unload Me
    End Sub
    I'm not sure what rs_employee_ae is here but it is not defined just like the error message tells you.

    The rs at the begining makes me think it is intended to be a recordset object but then you are refereencing the .text3.text which makes it look like it is a poorly named form object.

    At any rate when you get an error tellign you Variable not defined it means that either you did not define the variable it is showing you or you have misspelled it. Pretty obovious really.
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,875

    Re: Compile error: Variable not defined

    Here's an article, with sample code: here

    It shows how to open query and close a table.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jan 2012
    Posts
    14

    Re: Compile error: Variable not defined

    DataMiser,

    i have this code declared in PUBLIC in the other form (form1) "rs_employee_ae".

    Here it is:

    Option Explicit

    Public add_state As Boolean
    Public rs_employee_ae As New ADODB.Recordset
    Public Old_ID As String
    Public UPDATE_USER As Boolean
    Public Cat_Index As Long

  6. #6
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,784

    Re: Compile error: Variable not defined

    Code:
        With rs_employee_ae ' this code highlights the error
            .Cat_Index = ListView1.SelectedItem.ListSubItems(1)
            .Text3.Text = ListView1.SelectedItem.ListSubItems(2)
        End With
    If the var is defined as public in a different form then you must specify the formname as well.

    e.g.

    Code:
    With Form1.rs_employee_ae
    It would make more sense to declare the variable in a module if it truly needs to be public.

    Of course the next 2 lines are also going to generate errors as they are not properties of the recordset but appear to be objects on your other form.

    Look carefully at your code and you should see your mistakes, also remember that you must always specifiy the form name when trying to access items of a form from any source other than the form where they reside.
    Last edited by DataMiser; February 4th, 2012 at 02:07 AM.
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Jan 2012
    Posts
    14

    Re: Compile error: Variable not defined

    DataMiser,

    I've got your point and it's functioning now. (^_^)

    Thank you.

  8. #8
    Join Date
    Jan 2012
    Posts
    14

    Re: Compile error: Variable not defined

    dglienna,

    Thank you also.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center