how to store my value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zaankanter
    New Member
    • Feb 2008
    • 30

    how to store my value

    Hi,
    This is an updated version of my earlier post, hoping to attract new readers/answers.
    I want on opening my form, to check wether a field [res3] in the one-record table "programmavaria belen" is empty, and if it is, to fill it (that is of course, being the table designed to be one-record, in the first and only record) with a random integer.
    This is where Zwoker has lead me to so far:

    Code:
    Private Sub Form_Load()
    
    If IsNull(DLookup("[res3]", "programmavariabelen")) Then
        MsgBox "res3 is empty"
        Randomize
        MyTable.Open "programmavariabelen", CurrentProject.Connection, adOpenStatic, adLockOptimistic
        MyTable.MoveFirst
        MyTable![res3] = Rnd()
        MyTable.Update
        MyTable.Close
    
    MsgBox (Me![res3])
    End If
    End Sub
    It stops at My.table.open etc. , and states "object required"
    Does anyone see what we overlook?
  • Megalog
    Recognized Expert Contributor
    • Sep 2007
    • 378

    #2
    This is how I'd do it:

    Code:
    Private Sub Form_Load()
    Dim rs as DAO.Recordset
    
    If IsNull(DLookup("[res3]", "programmavariabelen")) Then
        MsgBox "res3 is empty"
        Randomize
        Set rs = CurrentDb.OpenRecordset("programmavariabelen", dbOpenDynaset)
        rs.MoveFirst
        rs.Edit
        rs![res3] = Rnd()
        rs.Update
        Set rs = nothing
        
        MsgBox (Me![res3])
    End If
    
    End Sub

    Comment

    • zaankanter
      New Member
      • Feb 2008
      • 30

      #3
      This is how I'd do it:


      Code: ( text )
      Private Sub Form_Load()
      Dim rs as DAO.Recordset

      If IsNull(DLookup( "[res3]", "programmavaria belen")) Then
      MsgBox "res3 is empty"
      Randomize
      Set rs = CurrentDb.OpenR ecordset("progr ammavariabelen" , dbOpenDynaset)
      rs.MoveFirst
      rs.Edit
      rs![res3] = Rnd()
      rs.Update
      Set rs = nothing

      MsgBox (Me![res3])
      End If

      End Sub
      This gives me an error in line 2: (translated from dutch its something like this:) "A datatype defined by user is not defined"

      Comment

      • zaankanter
        New Member
        • Feb 2008
        • 30

        #4
        The form in question is the first one to open on opening the acces-project. Does that make any difference?

        Comment

        • zaankanter
          New Member
          • Feb 2008
          • 30

          #5
          HI,

          I've stripped the programlines one by one by to find out what they do (and after stripping: don't).
          This is what I finaly got. And it works!
          Much, much, simpler than I imagened:
          Code:
          Private Sub Form_Load() 
          If IsNull(DLookup("[res3]", "programmavariabelen")) Then    
              Randomize    
              Me![res3] = Int(Rnd() * 10000000)   
              MsgBox (Me![res3])
          End If 
          End Sub
          It does exactly what I wanted it to do in the first place:
          - check if res3 is empty
          - then create a random number
          - and store it as res3

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32633

            #6
            Nicely done.

            You look as if you're having trouble finding the button that adds CODE tags. It's the # button.

            Welcome to The Scripts :)

            Comment

            Working...