Results 1 to 7 of 7
  1. #1
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Variables not declared (Access 2003)

    Having found a form not using Option Explicit, and putting it in, it's complaining about variables not being declared.

    Text3 = "comEventBreak" This is an item in an error list of an MSCOMM Control, any ideas how it should be declared?

    I use the syntax Cancel = True in some code, again how should it be declared

    Many thanks

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Variables not declared (Access 2003)

    comEventBreak is not a string, so there shouldn't be quotes around it.
    It's not clear to me why you would want to assign an error code to a text box.

    Cancel = True only makes sense in event procedures that have Cancel as argument, for example

    Private Sub Form_Open(Cancel As Integer)

    You shouldn't declare Cancel separately in such code. There is no point in using Cancel = True in event procedures (or other procedures) that don't have Cancel as argument - it won't do anything.

  3. #3
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Variables not declared (Access 2003)

    Thanks Hans. These two sections of code I have inherited in a program.

    Re below: There is no textbox on the form so I don't know what it does as it stands. There is no other mention of text3 in the project so maybe it was left in during design, and never reached this part of the program. It wasn't until I declared Option Explicit did it then come to my attention

    Select Case MSComm1.CommEvent

    Case comEventBreak ' A Break was received.
    Text3 = "comEventBreak"
    intComBreak = 1
    Case comEventFrame ' Framing Error
    Text3 = "comEventFrame"
    Case comEventOverrun ' Data Lost.
    Text3 = "comEventOverrun"
    Case comEventRxOver ' Receive buffer overflow.
    Text3 = "comEventRxOverFlow"
    Case comEvSend ' There are SThreshold number of
    Text3 = "comEvSend"
    ' characters in the transmit
    ' buffer.
    Case comEvEOF ' An EOF charater was found in
    Text3 = "comEvEOF"
    ' the input stream
    End Select


    This is where Cancel = True has been used. The code works, however Option Explicit showed its problem.

    Public Function NoGood() As Boolean

    Dim ctl As Control, DL As String
    Dim Msg As String, Style As Integer, Title As String, Response As Integer

    Set ctl = Screen.ActiveControl
    DL = vbNewLine & vbNewLine

    If IsNull(ctl) Then

    Response = MsgBox(prompt:="Fields have to be entered. Do you want to continue or cancel 'Yes' or 'No'.", Buttons:=vbYesNo)

    If Response = vbYes Then
    NoGood = True
    Set ctl = Nothing
    Else
    Me.Undo
    Cancel = True
    Me.Cancel2.SetFocus
    Cancel2_Click
    End If
    Set ctl = Nothing

    End If

    End Function

    Regards

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Variables not declared (Access 2003)

    The first piece of code requires that you have a reference to the MSComm ActiveX control (mscomm32.ocx) in Tools | References...
    Do you actually use the MSComm control?

    In the second piece of code, Cancel = True doesn't serve any discernible purpose, unless Cancel has been declared as a module level variable or as a public variable and if it is used elsewhere.

  5. #5
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Variables not declared (Access 2003)

    Thanks. I have removed the function where cancel was, as its not called for anywhere.

    Yes the MSCOMM control is in the project. I would rename Text3 to something else but I don't know what it carries, or what use it has, as there is no other reference to Text3. Only thing I am worried about is one of the case results sets something.

    I have come accross another - For Each ctl In Me.Controls. Do I Dim ctl as a control, object?

    Many thanks

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Variables not declared (Access 2003)

    1) If there is no other reference to Text3, you can probably delete or comment out the piece of code from Select Case to End Select without harm. The only thing the code does is setting the value of Text3.

    2) You should declare ctl as a Control:

    Dim ctl As Control

  7. #7
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Variables not declared (Access 2003)

    Many thanks Hans. Best regards

Posting Permissions

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