Connecting Tech Pros Worldwide Forums | Help | Site Map

how to store my value

Newbie
 
Join Date: Feb 2008
Posts: 30
#1: Mar 12 '08
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 "programmavariabelen" 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:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.  
  3. If IsNull(DLookup("[res3]", "programmavariabelen")) Then
  4.     MsgBox "res3 is empty"
  5.     Randomize
  6.     MyTable.Open "programmavariabelen", CurrentProject.Connection, adOpenStatic, adLockOptimistic
  7.     MyTable.MoveFirst
  8.     MyTable![res3] = Rnd()
  9.     MyTable.Update
  10.     MyTable.Close
  11.  
  12. MsgBox (Me![res3])
  13. End If
  14. End Sub
It stops at My.table.open etc. , and states "object required"
Does anyone see what we overlook?

Megalog's Avatar
Expert
 
Join Date: Sep 2007
Posts: 273
#2: Mar 12 '08

re: how to store my value


This is how I'd do it:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. Dim rs as DAO.Recordset
  3.  
  4. If IsNull(DLookup("[res3]", "programmavariabelen")) Then
  5.     MsgBox "res3 is empty"
  6.     Randomize
  7.     Set rs = CurrentDb.OpenRecordset("programmavariabelen", dbOpenDynaset)
  8.     rs.MoveFirst
  9.     rs.Edit
  10.     rs![res3] = Rnd()
  11.     rs.Update
  12.     Set rs = nothing
  13.  
  14.     MsgBox (Me![res3])
  15. End If
  16.  
  17. End Sub
Newbie
 
Join Date: Feb 2008
Posts: 30
#3: Mar 12 '08

re: how to store my value


Quote:
This is how I'd do it:


Code: ( text )
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
This gives me an error in line 2: (translated from dutch its something like this:) "A datatype defined by user is not defined"
Newbie
 
Join Date: Feb 2008
Posts: 30
#4: Mar 13 '08

re: how to store my value


The form in question is the first one to open on opening the acces-project. Does that make any difference?
Newbie
 
Join Date: Feb 2008
Posts: 30
#5: Mar 13 '08

re: how to store my value


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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load() 
  2. If IsNull(DLookup("[res3]", "programmavariabelen")) Then    
  3.     Randomize    
  4.     Me![res3] = Int(Rnd() * 10000000)   
  5.     MsgBox (Me![res3])
  6. End If 
  7. 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
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,747
#6: Mar 13 '08

re: how to store my value


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 :)
Reply