Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2009
    Location
    Ohio, United States
    Posts
    167

    Unanswered: How to set up unbound form text box to use as variable

    I'd like to include an unbound text box on a form that retains the value entered, even after it has been closed and reopened. I'd also want to use that text as a variable in my VBA code. I've heard this is not possible with unbound text boxes but still wanted to check.

    I had also heard you can create a table with a single record and just reference that table. However, I was having trouble doing that because the table was a different record source than what the rest of the form was using. Should I just create a separate form for this or is there another way.

  2. #2
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    You could load the value into the textbox in the form's load event, using a DLookup or recordset to get the value from the table.
    Paul

  3. #3
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Table name: Tbl_Persistent_Objects
    SysCounter: Autonumber, Primary key
    Control_Name: Text 255, Indexed no duplicates
    Control_Value: Text 255

    In an independent module:

    Code:
    Public Function Serialize(Ctl As Control) As Long
    
        Dim strSQL As String
        
        strSQL = "UPDATE Tbl_Persistent_Objects SET Control_Value = '" & Ctl.Value & "' " & _
                "WHERE SysCounter = " & Ctl.Tag
        CurrentDb.Execute strSQL, dbSeeChanges
        Serialize = True
        
    End Function
    
    Public Function DeSerialize(Ctl As Control) As Long
    
        Ctl.Value = DLookup("Control_Value", "Tbl_Persistent_Objects", "SysCounter = " & Ctl.Tag)
        DeSerialize = True
        
    End Function
    Or:
    Code:
    Public Function Serialize(Ctl As Control) As Long
    
        Dim strSQL As String
        
        strSQL = "UPDATE Tbl_Persistent_Objects SET Control_Value = '" & Ctl.Value & "'" & _
                "WHERE Control_Name = '" & Ctl.Name & "'"
        CurrentDb.Execute strSQL, dbSeeChanges
        Serialize = True
    
    End Function
    
    Public Function DeSerialize(Ctl As Control) As Long
    
        Ctl.Value = DLookup("Control_Value", "Tbl_Persistent_Objects", "Control_Name = '" & Ctl.Name & "'")
        DeSerialize = True
        
    End Function
    Then:
    Code:
    Private Sub Form_Close()
    
        Serialize Me.Text_1
        
    End Sub
    
    Private Sub Form_Open(Cancel As Integer)
    
        DeSerialize Me.Text_1
        
    End Sub
    Have a nice day!

  4. #4
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    Sinddho, you rock!
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

  5. #5
    Join Date
    Jan 2009
    Location
    Ohio, United States
    Posts
    167
    THANK YOU!! That was exactly what I was after! Joshua

  6. #6
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    You're welcome.

    Have a nice day!

Posting Permissions

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