Random day of Week

R

Ripper

I am trying to use a code I got from the board to display a random day of the week in an unbound textbox. I tried to change the code a bit, but I have NEGATIVE skill in this catagory. The line I marked with an arrow keeps getting an error and I tried everything I can think of. Anyone able to help

Thanks As Always
RI

Private Sub Form_Load(
'Highest form numbe
Const lngcUpperBound =

'Lowest form numbe
Const lngcLowerBound =

Dim lngFormNumber As Lon
Dim strDayofWeek As Strin

'Requires reference to Microsoft DAO 3.6 Object Library (or 3.51 for Access 97)
Dim db As DAO.Databas
Dim rst As DAO.Recordse

'Generate a random number in the specified range. See 'Rnd Function' in the help file for details
Randomize Time
lngFormNumber = Int((lngcUpperBound - lngcLowerBound + 1) * Rnd + lngcLowerBound

'Open a recordset to get the form name corresponding to the randomnumber
Set db = CurrentD
--> Set rst = db.OpenRecordset("SELECT [Day] FROM tblDaysofWeek WHERE [Day] = " & lngFormNumber) <--- Error HER
strDayofWeek = rst.Fields("Day"
rst.Close
 
D

Douglas J. Steele

What's the makeup of tblDaysofWeek? Is Day supposed to be Monday, Tuesday,
Wednesday... or a number between 1 and 7?


--
Doug Steele, Microsoft Access MVP

(No private e-mails, please)



Ripper said:
I am trying to use a code I got from the board to display a random day of
the week in an unbound textbox. I tried to change the code a bit, but I
have NEGATIVE skill in this catagory. The line I marked with an arrow keeps
getting an error and I tried everything I can think of. Anyone able to
help?
Thanks As Always,
RIP

Private Sub Form_Load()
'Highest form number
Const lngcUpperBound = 7

'Lowest form number
Const lngcLowerBound = 1

Dim lngFormNumber As Long
Dim strDayofWeek As String

'Requires reference to Microsoft DAO 3.6 Object Library (or 3.51 for Access 97).
Dim db As DAO.Database
Dim rst As DAO.Recordset

'Generate a random number in the specified range. See 'Rnd Function' in the help file for details.
Randomize Timer
lngFormNumber = Int((lngcUpperBound - lngcLowerBound + 1) * Rnd + lngcLowerBound)

'Open a recordset to get the form name corresponding to the randomnumber.
Set db = CurrentDb
--> Set rst = db.OpenRecordset("SELECT [Day] FROM tblDaysofWeek WHERE
[Day] = " & lngFormNumber) <--- Error HERE
 
R

Ripper

Day is Monday, Tuesday, etc ID is a number 1 = Sunday 2 = Monday, etc
----- Douglas J. Steele wrote: ----

What's the makeup of tblDaysofWeek? Is Day supposed to be Monday, Tuesday
Wednesday... or a number between 1 and 7


--
Doug Steele, Microsoft Access MV
http://I.Am/DougSteel
(No private e-mails, please



Ripper said:
I am trying to use a code I got from the board to display a random day o
the week in an unbound textbox. I tried to change the code a bit, but
have NEGATIVE skill in this catagory. The line I marked with an arrow keep
getting an error and I tried everything I can think of. Anyone able t
help
Thanks As Always RI
Private Sub Form_Load(
'Highest form numbe
Const lngcUpperBound =
'Lowest form numbe Const lngcLowerBound =
Dim lngFormNumber As Lon Dim strDayofWeek As Strin
'Requires reference to Microsoft DAO 3.6 Object Library (or 3.51 fo
Access 97)
Dim db As DAO.Databas
Dim rst As DAO.Recordse
'Generate a random number in the specified range. See 'Rnd Function' i
the help file for details
Randomize Time
lngFormNumber = Int((lngcUpperBound - lngcLowerBound + 1) * Rnd lngcLowerBound
'Open a recordset to get the form name corresponding to th
randomnumber
Set db = CurrentD
--> Set rst = db.OpenRecordset("SELECT [Day] FROM tblDaysofWeek WHER
[Day] = " & lngFormNumber) <--- Error HER
 
C

Chris

Set rst = db.OpenRecordset("SELECT [Day] FROM
tblDaysofWeek WHERE [Day] = Weekday(" & lngFormNumber
& ")")


Chris

-----Original Message-----

Day is Monday, Tuesday, etc ID is a number 1 = Sunday 2 = Monday, etc.
----- Douglas J. Steele wrote: -----

What's the makeup of tblDaysofWeek? Is Day supposed to be Monday, Tuesday,
Wednesday... or a number between 1 and 7?


--
Doug Steele, Microsoft Access MVP

(No private e-mails, please)



I am trying to use a code I got from the board to
display a random day of
the week in an unbound textbox. I tried to change the code a bit, but I
have NEGATIVE skill in this catagory. The line I marked with an arrow keeps
getting an error and I tried everything I can think of. Anyone able to
help?
'Highest form number
Const lngcUpperBound = 7
Object Library (or 3.51 for
Access 97).
Dim db As DAO.Database
Dim rst As DAO.Recordset
range. See 'Rnd Function' in
the help file for details.
Randomize Timer
lngFormNumber = Int((lngcUpperBound -
lngcLowerBound + 1) * Rnd +
lngcLowerBound) corresponding to the
randomnumber.
Set db = CurrentDb
--> Set rst = db.OpenRecordset("SELECT [Day] FROM
tblDaysofWeek WHERE
[Day] = " & lngFormNumber) <--- Error HERE
strDayofWeek = rst.Fields("Day")
rst.Close



.
 
R

Ripper

Ok, that line works, but now I cannot get the name of the field into the textbox.
Anyone want to attempt another shot

Thanks
RI


'Highest form numbe
Const lngcUpperBound =

'Lowest form numbe
Const lngcLowerBound =

Dim lngFormNumber As Lon
Dim strRandomDay As Strin

'Requires reference to Microsoft DAO 3.6 Object Library (or 3.51 for Access 97)
Dim db As DAO.Databas
Dim rst As DAO.Recordse

'Generate a random number in the specified range. See 'Rnd Function' in the help file for details
Randomize Time
lngFormNumber = Int((lngcUpperBound - lngcLowerBound + 1) * Rnd + lngcLowerBound

'Open a recordset to get the form name corresponding to the randomnumber
Set db = CurrentD
Set rst = db.OpenRecordset("SELECT Day FROM tblDaysofWeek WHERE Day = Weekday(" & lngFormNumber & ")"

---> WHAT DO I PUT HERE TO MAKE THE FIELD tblData.RandomDay = the day of the week selected in the line above
rst.Close
 
C

Chris Nebinger

I'm a little lost as to what you are trying to do...
Do want to save the day in a field somewhere?
Or put it in a text box?

Also, because Day is a reserved Access Keyword, you'll
need []'s around them:

Set rst = db.OpenRecordset("SELECT [Day] FROM
tblDaysofWeek WHERE [Day] = Weekday(" & lngFormNumber
& ")")

Does tblDaysOfWeek only contain 7 records? Sunday,
Monday, etc.?

Looking back at your code, I'm really lost as to what your
goal is...



Chris Nebinger




-----Original Message-----
Ok, that line works, but now I cannot get the name of the field into the textbox.
Anyone want to attempt another shot?

Thanks,
RIP



'Highest form number
Const lngcUpperBound = 7

'Lowest form number
Const lngcLowerBound = 1

Dim lngFormNumber As Long
Dim strRandomDay As String

'Requires reference to Microsoft DAO 3.6 Object
Library (or 3.51 for Access 97).
Dim db As DAO.Database
Dim rst As DAO.Recordset

'Generate a random number in the specified range.
See 'Rnd Function' in the help file for details.
Randomize Timer
lngFormNumber = Int((lngcUpperBound - lngcLowerBound + 1) * Rnd + lngcLowerBound)

'Open a recordset to get the form name corresponding to the randomnumber.
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT Day FROM
tblDaysofWeek WHERE Day = Weekday(" & lngFormNumber & ")")
---> WHAT DO I PUT HERE TO MAKE THE FIELD
tblData.RandomDay = the day of the week selected in the
line above?
 
R

Ripper

Chris, Thanks for hanging in with me

I am designing this dBase for some Special Ed students. They need much more repetition to help remember things so this dBase will help them achieve that

I have a form, frmDayOfWeek, that has two fields on it, Today and DayAfter. I want a random day of the week to appear in the field Today, stored so I can use it on the report, and then the students will type in whatever the next day of the week is in the field DayAfter

I have a table, tblDaysOfWeek, that has and ID field and a Day field. The ID range from 1 to 7 and the correspont with the day of the week. 1=Sunday, 2=Monday, 3=Wednesday, etc

I thought the code would generate a random number, go get the day of the week from the tblDaysOfWeek, and put that day in the field Today on the current form

Am I in the right ballpark or is there a better way to do what I want

Thanks Chris
RI
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top