Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2004
    Location
    London
    Posts
    2

    Unanswered: Public Variables Loosing Values

    I have a set of variable that keeps loosing its value for no apparent reason.

    I've declared the variable in a standard module:

    [vbcode]
    Option Compare Database
    Option Explicit

    ' Declare User variables
    Public strCurrentLogID As String
    [/vbcode]

    I've set it in a public function in the same module:

    [vbcode]

    strCurrentLogID = Forms!frmlog.fldlogid

    [/vbcode]

    But when set Watch and use a combo box in a form, the variable turns to "", even though there's nothing in the combo box event that should do this:

    [vbcode]
    Private Sub cboDocCodeFilter_AfterUpdate()

    On Error GoTo ErrorHandler

    Forms![frmSearch].[fldStringSearch].Value = "*"
    Me.frmSub.Requery

    End

    ErrorHandlerExit:

    Exit Sub

    ErrorHandler:

    Call prcErrorMessager

    Resume ErrorHandlerExit

    End Sub
    [/vbcode]

    What's making this variable turn to "". Can any one help?

    Thanks

    Zahid

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Hi Zahid

    That's a long time between joining and your first post - glad you found a use for us
    A quickie - the tags are [ CODE] [ /CODE] (without the spaces) - you can use the hash\ pound sign to insert them for you.

    Anyhoo - public variables lose their values if an end statement is executed or the reset button pressed.

    In anycase, I tend to use something like the below to counter any loss of variable values:

    Code:
     
    Private MyString as String
     
    Public Function fGetMyString as String
     
    If MyString <> "" Then
    fGetMyString = MyString
    Else
    fGetMyString = Forms!MyForm!MyTextBox
    End If
     
    End Function
    This way, you can't accidently clear the value or set it to something invalid (it is private - it can only be accessed via the function) plus the value is reset in the event of a crash. A bit like a poor mans class property

    HTH
    Last edited by pootle flump; 09-30-05 at 08:11.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  3. #3
    Join Date
    Feb 2004
    Location
    London
    Posts
    2
    Yup!! that was the answer. I found an End statement lurking in a dark corner of a function.

    Sorted!

    Thank you very much!!

    I will remember the correct tags for future reference

    Regards

    Zahid

  4. #4
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Hi Zahid

    Glad it sorted it. BTW - (mostly because of this) END statements aren't considered very good practice - it might be an idea to search your code for any more and remove them or replace with, say, an exit sub or whatever might be appropriate

    HTH
    Testimonial:
    pootle flump
    ur codings are working excelent.

Posting Permissions

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