forms and variables help please

  • Thread starter lee_cufc via AccessMonster.com
  • Start date
L

lee_cufc via AccessMonster.com

hi guys,

wondering if anyone can help with the following?

[Forms]![NEW]![(Dates.Colour)].[BackColor] = [vbRed]

In this code i'm wanting it to go to the form named 'NEW' and change the back
color of a textbox to red.
The name of the text box is stored in the global variable 'colour', which is
declared in the module 'dates' (hence the dates.colour part)
On stepping through my code i know that the name of the textbox is being
stored correctly in the variable im just unsure of how to use this code with
a variable in?

Any thoughts or ideas?
thanks
Lee
 
L

lee_cufc via AccessMonster.com

Jeff,

Thanks for the reply, the text box i am trying to change is on the form 'NEW'
which is a different one to the one in which this code is contained.

The form 'NEW' is also open at the same time however

Lee

Jeff said:
Where are you trying to do this?

If you are already in the form, you could use something like:

Me!txtYourTextBox.BackColor = vbRed

(notice that there are no square brackets)

If you are trying to do this from another form, you'd need to have your form
named "NEW" already open, I suspect.

More info, please...
[quoted text clipped - 13 lines]
thanks
Lee
 
D

Douglas J. Steele

To refer to a control on the New form where the control name is a variable,
you'd use

Forms![New].Controls(variable)

If Colour is declared as a public variable in a module, you should be able
to refer to it simply as Colour, not as Dates.Colour. The only reason you'd
need to qualify with the module name is if you've got Colour declared as a
public variable in more than one module, which is not a very good practice.

Try simply using

Forms![NEW].Controls(Colour).BackColor = vbRed

Note that most of the square brackets you had are unnecessary (and, in fact,
may be incorrect)
 
J

Jeff Boyce

Where are you trying to do this?

If you are already in the form, you could use something like:

Me!txtYourTextBox.BackColor = vbRed

(notice that there are no square brackets)

If you are trying to do this from another form, you'd need to have your form
named "NEW" already open, I suspect.

More info, please...

--
Regards

Jeff Boyce
www.InformationFutures.net

Microsoft Office/Access MVP


Microsoft IT Academy Program Mentor
http://microsoftitacademy.com/
 
L

lee_cufc via AccessMonster.com

Douglas,

Many thanks for your help, it works perfectly, however now i have run into a
problem i didnt realise would occur.

I need it to also search out a specific record so i have been trying to
modify your code as follows

Forms![NEW].record(Dates2).Controls(Colour).BackColor = vbRed

where (Dates2) holds the record that i need to find, i guess i am writing the
code out wrong as i am getting an error message, is it possible to do
something like .record??

thanks
Lee
To refer to a control on the New form where the control name is a variable,
you'd use

Forms![New].Controls(variable)

If Colour is declared as a public variable in a module, you should be able
to refer to it simply as Colour, not as Dates.Colour. The only reason you'd
need to qualify with the module name is if you've got Colour declared as a
public variable in more than one module, which is not a very good practice.

Try simply using

Forms![NEW].Controls(Colour).BackColor = vbRed

Note that most of the square brackets you had are unnecessary (and, in fact,
may be incorrect)
[quoted text clipped - 16 lines]
thanks
Lee
 
D

Douglas J. Steele

There is no Record collection (or property) for forms.

Take a look at the Bookmark property to allow you to indicate which row.

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


lee_cufc via AccessMonster.com said:
Douglas,

Many thanks for your help, it works perfectly, however now i have run into
a
problem i didnt realise would occur.

I need it to also search out a specific record so i have been trying to
modify your code as follows

Forms![NEW].record(Dates2).Controls(Colour).BackColor = vbRed

where (Dates2) holds the record that i need to find, i guess i am writing
the
code out wrong as i am getting an error message, is it possible to do
something like .record??

thanks
Lee
To refer to a control on the New form where the control name is a
variable,
you'd use

Forms![New].Controls(variable)

If Colour is declared as a public variable in a module, you should be able
to refer to it simply as Colour, not as Dates.Colour. The only reason
you'd
need to qualify with the module name is if you've got Colour declared as a
public variable in more than one module, which is not a very good
practice.

Try simply using

Forms![NEW].Controls(Colour).BackColor = vbRed

Note that most of the square brackets you had are unnecessary (and, in
fact,
may be incorrect)
[quoted text clipped - 16 lines]
thanks
Lee
 
L

lee_cufc via AccessMonster.com

Forms![NEW].[DateOfAppointment].Bookmark(DateOfBooking).Controls(Colour).
BackColor = vbRed

I have amended it to this but still cant figure out how to get it to work,
In the code above [DateOfAppointment] is the field on the form i need to
bookmark by (DateOfBooking)

Any Ideas?
There is no Record collection (or property) for forms.

Take a look at the Bookmark property to allow you to indicate which row.
[quoted text clipped - 41 lines]
 
D

Douglas J. Steele

You don't use bookmarks like that.

For an example of what you need to do, see
http://msdn.microsoft.com/en-us/library/bb237308.aspx

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


lee_cufc via AccessMonster.com said:
Forms![NEW].[DateOfAppointment].Bookmark(DateOfBooking).Controls(Colour).
BackColor = vbRed

I have amended it to this but still cant figure out how to get it to work,
In the code above [DateOfAppointment] is the field on the form i need to
bookmark by (DateOfBooking)

Any Ideas?
There is no Record collection (or property) for forms.

Take a look at the Bookmark property to allow you to indicate which row.
[quoted text clipped - 41 lines]
thanks
Lee
 
L

lee_cufc via AccessMonster.com

I have had a looked at the article and have played around with the code for a
couple of hours now but unfortunately i am still stuck, apologies i am a very
basic user of Access :-(
Can anyone help me by giving me the correct layout for the code specific to
what i need it to achieve, this would be so much appreciated as i am fast
approaching a deadline for a school project,

Many thanks
Lee
You don't use bookmarks like that.

For an example of what you need to do, see
http://msdn.microsoft.com/en-us/library/bb237308.aspx
Forms![NEW].[DateOfAppointment].Bookmark(DateOfBooking).Controls(Colour).
BackColor = vbRed
[quoted text clipped - 14 lines]
 
L

lee_cufc via AccessMonster.com

Anyone??


lee_cufc said:
I have had a looked at the article and have played around with the code for a
couple of hours now but unfortunately i am still stuck, apologies i am a very
basic user of Access :-(
Can anyone help me by giving me the correct layout for the code specific to
what i need it to achieve, this would be so much appreciated as i am fast
approaching a deadline for a school project,

Many thanks
Lee
You don't use bookmarks like that.
[quoted text clipped - 6 lines]
 
B

BruceM

A lot of people are here during the week only, so Sunday may be a slow day
for responses. Also, some people here, including me, are reluctant to
provide a lot of information when it turns out the question relates to a
school project, as it seems like doing somebody else's homework.

Having said that, if the coloring works correctly, it shouldn't matter how
you get to the record. Use Bookmark to go to the record, after which the
code to color the control should work as it did before. In the example,
Bookmark uses an input box for the search parameters, but you could also use
a text box or combo box on the form.

Also, the sample code will find only the first instance of code that matches
the criteria in the output box. If you type "Bill" to search for "Billy",
and there is a record for "Bill" you will not get a chance to see "Billy" by
way of the search.

As a general thing, if you are having trouble with code, post what you have
tried.

lee_cufc via AccessMonster.com said:
Anyone??


lee_cufc said:
I have had a looked at the article and have played around with the code
for a
couple of hours now but unfortunately i am still stuck, apologies i am a
very
basic user of Access :-(
Can anyone help me by giving me the correct layout for the code specific
to
what i need it to achieve, this would be so much appreciated as i am fast
approaching a deadline for a school project,

Many thanks
Lee
You don't use bookmarks like that.
[quoted text clipped - 6 lines]
thanks
Lee
 
L

lee_cufc via AccessMonster.com

Bruce,

Thanks For your reply,

Im sorry if I was a little unclear, my project is for school however what im
doing has not been set out by the school in any way, its all entirely my own
project, I wasn’t simply looking for someone to do my homework but just for
some help to try and achieve something that I’ve decided I’d like to put into
my system.

As mentioned earlier the code I have written and been trying is:

Forms![NEW].[DateOfAppointment].Bookmark(DateOfBooking).Controls(Colour).
BackColor = vbRed

However this is returning an error. I have read your comments and the
tutorial link posted on this thread however i still do not understand, I
realise I need to use the bookmark function to find the record however what I
am trying to say is that I have no idea of the correct syntax of the .
bookmark section of the code above as I cannot find a similar example
anywhere to work from. I have tried and tried different combinations of the
code above but nothing has worked so far and so I was just looking to see if
anyone could tell me how to change the line of code I’ve got around to get
it to function correctly.


Thanks
Lee

A lot of people are here during the week only, so Sunday may be a slow day
for responses. Also, some people here, including me, are reluctant to
provide a lot of information when it turns out the question relates to a
school project, as it seems like doing somebody else's homework.

Having said that, if the coloring works correctly, it shouldn't matter how
you get to the record. Use Bookmark to go to the record, after which the
code to color the control should work as it did before. In the example,
Bookmark uses an input box for the search parameters, but you could also use
a text box or combo box on the form.

Also, the sample code will find only the first instance of code that matches
the criteria in the output box. If you type "Bill" to search for "Billy",
and there is a record for "Bill" you will not get a chance to see "Billy" by
way of the search.

As a general thing, if you are having trouble with code, post what you have
tried.
[quoted text clipped - 16 lines]
 
B

BruceM

As Douglas said, the code you are trying is not the way to use Bookmark. My
suggestion was to find the record. If the formatting is working now, it
should work when you find the record. Do not attempt to find the record and
apply formatting at the same time.

The sample code provides the syntax using an input box:

********************
Private Sub cmdFindContactName_Click()

Dim rst As DAO.Recordset
Dim strCriteria As String

strCriteria = "[ContactName] Like '*" & InputBox("Enter the " _
& "first few letters of the name to find") & "*'"

Set rst = Me.RecordsetClone
rst.FindFirst strCriteria
If rst.NoMatch Then
MsgBox "No entry found.", vbInformation
Else
Me.Bookmark = rst.Bookmark
End If

Set rst = Nothing

End Sub

********************
You can use another way to select the criteria by which you would find the
record, but the only way I or anybody else can help is to know what that is.
How are you determining which record to find? Text box? Combo box?


lee_cufc via AccessMonster.com said:
Bruce,

Thanks For your reply,

Im sorry if I was a little unclear, my project is for school however what
im
doing has not been set out by the school in any way, its all entirely my
own
project, I wasn't simply looking for someone to do my homework but just
for
some help to try and achieve something that I've decided I'd like to put
into
my system.

As mentioned earlier the code I have written and been trying is:

Forms![NEW].[DateOfAppointment].Bookmark(DateOfBooking).Controls(Colour).
BackColor = vbRed

However this is returning an error. I have read your comments and the
tutorial link posted on this thread however i still do not understand, I
realise I need to use the bookmark function to find the record however
what I
am trying to say is that I have no idea of the correct syntax of the .
bookmark section of the code above as I cannot find a similar example
anywhere to work from. I have tried and tried different combinations of
the
code above but nothing has worked so far and so I was just looking to see
if
anyone could tell me how to change the line of code I've got around to
get
it to function correctly.


Thanks
Lee

A lot of people are here during the week only, so Sunday may be a slow day
for responses. Also, some people here, including me, are reluctant to
provide a lot of information when it turns out the question relates to a
school project, as it seems like doing somebody else's homework.

Having said that, if the coloring works correctly, it shouldn't matter how
you get to the record. Use Bookmark to go to the record, after which the
code to color the control should work as it did before. In the example,
Bookmark uses an input box for the search parameters, but you could also
use
a text box or combo box on the form.

Also, the sample code will find only the first instance of code that
matches
the criteria in the output box. If you type "Bill" to search for "Billy",
and there is a record for "Bill" you will not get a chance to see "Billy"
by
way of the search.

As a general thing, if you are having trouble with code, post what you
have
tried.
[quoted text clipped - 16 lines]
thanks
Lee
 
L

lee_cufc via AccessMonster.com

Bruce,

the way in which i want to determine which record to find is by using the
value stored in the global variable 'Dates2' this will be a date, i then want
to pull up the record which has the matching date in the 'Date Of
Appointment' field and then apply the formatting to that field

I understand that your saying i should locate the record first and then apply
the formatting code that i know works however as the record is on a different
form im not sure how to go about this, i.e. i guess 'Me.recordset' etc wont
work?

Lee
As Douglas said, the code you are trying is not the way to use Bookmark. My
suggestion was to find the record. If the formatting is working now, it
should work when you find the record. Do not attempt to find the record and
apply formatting at the same time.

The sample code provides the syntax using an input box:

********************
Private Sub cmdFindContactName_Click()

Dim rst As DAO.Recordset
Dim strCriteria As String

strCriteria = "[ContactName] Like '*" & InputBox("Enter the " _
& "first few letters of the name to find") & "*'"

Set rst = Me.RecordsetClone
rst.FindFirst strCriteria
If rst.NoMatch Then
MsgBox "No entry found.", vbInformation
Else
Me.Bookmark = rst.Bookmark
End If

Set rst = Nothing

End Sub

********************
You can use another way to select the criteria by which you would find the
record, but the only way I or anybody else can help is to know what that is.
How are you determining which record to find? Text box? Combo box?
[quoted text clipped - 60 lines]
 
B

BruceM

You can use the date variable as the criteria:

Private Sub cmdFindContactName_Click()

Dim rst As DAO.Recordset
Dim strCriteria As String

strCriteria = "[DateField] = " & datYourVariable
Set rst = Me.RecordsetClone
rst.FindFirst strCriteria
If rst.NoMatch Then
MsgBox "No entry found.", vbInformation
Else
Me.Bookmark = rst.Bookmark
End If

Set rst = Nothing

End Sub

Are you trying to format a control on one form based on a value on another
form? Is there any connection between the data on one form and the data on
the other one? Please decribe in plain language, without database code or
terminology, exactly what you are trying to accomplish.

lee_cufc via AccessMonster.com said:
Bruce,

the way in which i want to determine which record to find is by using the
value stored in the global variable 'Dates2' this will be a date, i then
want
to pull up the record which has the matching date in the 'Date Of
Appointment' field and then apply the formatting to that field

I understand that your saying i should locate the record first and then
apply
the formatting code that i know works however as the record is on a
different
form im not sure how to go about this, i.e. i guess 'Me.recordset' etc
wont
work?

Lee
As Douglas said, the code you are trying is not the way to use Bookmark.
My
suggestion was to find the record. If the formatting is working now, it
should work when you find the record. Do not attempt to find the record
and
apply formatting at the same time.

The sample code provides the syntax using an input box:

********************
Private Sub cmdFindContactName_Click()

Dim rst As DAO.Recordset
Dim strCriteria As String

strCriteria = "[ContactName] Like '*" & InputBox("Enter the " _
& "first few letters of the name to find") & "*'"

Set rst = Me.RecordsetClone
rst.FindFirst strCriteria
If rst.NoMatch Then
MsgBox "No entry found.", vbInformation
Else
Me.Bookmark = rst.Bookmark
End If

Set rst = Nothing

End Sub

********************
You can use another way to select the criteria by which you would find the
record, but the only way I or anybody else can help is to know what that
is.
How are you determining which record to find? Text box? Combo box?
[quoted text clipped - 60 lines]
thanks
Lee
 
D

Douglas J. Steele

Not quite, Bruce.

strCriteria = "[DateField] = " & Format(datYourVariable, "\#yyyy\-mm\-dd\#")


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


BruceM said:
You can use the date variable as the criteria:

Private Sub cmdFindContactName_Click()

Dim rst As DAO.Recordset
Dim strCriteria As String

strCriteria = "[DateField] = " & datYourVariable
Set rst = Me.RecordsetClone
rst.FindFirst strCriteria
If rst.NoMatch Then
MsgBox "No entry found.", vbInformation
Else
Me.Bookmark = rst.Bookmark
End If

Set rst = Nothing

End Sub

Are you trying to format a control on one form based on a value on another
form? Is there any connection between the data on one form and the data
on the other one? Please decribe in plain language, without database code
or terminology, exactly what you are trying to accomplish.

lee_cufc via AccessMonster.com said:
Bruce,

the way in which i want to determine which record to find is by using the
value stored in the global variable 'Dates2' this will be a date, i then
want
to pull up the record which has the matching date in the 'Date Of
Appointment' field and then apply the formatting to that field

I understand that your saying i should locate the record first and then
apply
the formatting code that i know works however as the record is on a
different
form im not sure how to go about this, i.e. i guess 'Me.recordset' etc
wont
work?

Lee
As Douglas said, the code you are trying is not the way to use Bookmark.
My
suggestion was to find the record. If the formatting is working now, it
should work when you find the record. Do not attempt to find the record
and
apply formatting at the same time.

The sample code provides the syntax using an input box:

********************
Private Sub cmdFindContactName_Click()

Dim rst As DAO.Recordset
Dim strCriteria As String

strCriteria = "[ContactName] Like '*" & InputBox("Enter the " _
& "first few letters of the name to find") & "*'"

Set rst = Me.RecordsetClone
rst.FindFirst strCriteria
If rst.NoMatch Then
MsgBox "No entry found.", vbInformation
Else
Me.Bookmark = rst.Bookmark
End If

Set rst = Nothing

End Sub

********************
You can use another way to select the criteria by which you would find
the
record, but the only way I or anybody else can help is to know what that
is.
How are you determining which record to find? Text box? Combo box?

Bruce,

[quoted text clipped - 60 lines]
thanks
Lee
 
B

BruceM

Is that because strCriteria is a string variable, so the date variable needs
to be converted to a string value? If so, why that particular formatting?
I looked back through the thread, but could not find anything specific about
the date variable other than that it was global.

Douglas J. Steele said:
Not quite, Bruce.

strCriteria = "[DateField] = " & Format(datYourVariable,
"\#yyyy\-mm\-dd\#")


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


BruceM said:
You can use the date variable as the criteria:

Private Sub cmdFindContactName_Click()

Dim rst As DAO.Recordset
Dim strCriteria As String

strCriteria = "[DateField] = " & datYourVariable
Set rst = Me.RecordsetClone
rst.FindFirst strCriteria
If rst.NoMatch Then
MsgBox "No entry found.", vbInformation
Else
Me.Bookmark = rst.Bookmark
End If

Set rst = Nothing

End Sub

Are you trying to format a control on one form based on a value on
another form? Is there any connection between the data on one form and
the data on the other one? Please decribe in plain language, without
database code or terminology, exactly what you are trying to accomplish.

lee_cufc via AccessMonster.com said:
Bruce,

the way in which i want to determine which record to find is by using
the
value stored in the global variable 'Dates2' this will be a date, i then
want
to pull up the record which has the matching date in the 'Date Of
Appointment' field and then apply the formatting to that field

I understand that your saying i should locate the record first and then
apply
the formatting code that i know works however as the record is on a
different
form im not sure how to go about this, i.e. i guess 'Me.recordset' etc
wont
work?

Lee

BruceM wrote:
As Douglas said, the code you are trying is not the way to use Bookmark.
My
suggestion was to find the record. If the formatting is working now, it
should work when you find the record. Do not attempt to find the record
and
apply formatting at the same time.

The sample code provides the syntax using an input box:

********************
Private Sub cmdFindContactName_Click()

Dim rst As DAO.Recordset
Dim strCriteria As String

strCriteria = "[ContactName] Like '*" & InputBox("Enter the " _
& "first few letters of the name to find") & "*'"

Set rst = Me.RecordsetClone
rst.FindFirst strCriteria
If rst.NoMatch Then
MsgBox "No entry found.", vbInformation
Else
Me.Bookmark = rst.Bookmark
End If

Set rst = Nothing

End Sub

********************
You can use another way to select the criteria by which you would find
the
record, but the only way I or anybody else can help is to know what that
is.
How are you determining which record to find? Text box? Combo box?

Bruce,

[quoted text clipped - 60 lines]
thanks
Lee
 
D

Douglas J. Steele

I suppose you could say that it's because of the fact that strCriteria is a
string.

While internally dates are numbers (eight byte floating points, where the
integer portion represents the date as the number of days relative to 30
Dec, 1899 and the decimal portion represents the time as a fraction of a
day), if you concatenate a date value to a string, you'll going to get the
date written out as a date, using whatever Short Date format has been
defined through Regional Settings. If that format happens to be dd/mm/yyyy,
for 01 May, 2009 you'd get

[DateField] = 01/05/2009

When dealing with date values in Where clauses, they need to be delimited
with octothorpes (#), and the date must be formatted in a manner that Access
will recognize. Generally, that means mm/dd/yyyy format or yyyy-mm-dd
format, although other unambiguous formats could be used (dd mmm yyyy). Even
if you use

strCriteria = "[DateField] = #" & datYourVariable & "#"

so that you have

[DateField] = #01/05/2009#

it won't work. Regardless of the fact that the Short Date format is set to
dd/mm/yyyy, Access will treat that as 05 January, 2009.

That's the reason for using an explicit format.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


BruceM said:
Is that because strCriteria is a string variable, so the date variable
needs to be converted to a string value? If so, why that particular
formatting? I looked back through the thread, but could not find anything
specific about the date variable other than that it was global.

Douglas J. Steele said:
Not quite, Bruce.

strCriteria = "[DateField] = " & Format(datYourVariable,
"\#yyyy\-mm\-dd\#")


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


BruceM said:
You can use the date variable as the criteria:

Private Sub cmdFindContactName_Click()

Dim rst As DAO.Recordset
Dim strCriteria As String

strCriteria = "[DateField] = " & datYourVariable
Set rst = Me.RecordsetClone
rst.FindFirst strCriteria
If rst.NoMatch Then
MsgBox "No entry found.", vbInformation
Else
Me.Bookmark = rst.Bookmark
End If

Set rst = Nothing

End Sub

Are you trying to format a control on one form based on a value on
another form? Is there any connection between the data on one form and
the data on the other one? Please decribe in plain language, without
database code or terminology, exactly what you are trying to accomplish.

Bruce,

the way in which i want to determine which record to find is by using
the
value stored in the global variable 'Dates2' this will be a date, i
then want
to pull up the record which has the matching date in the 'Date Of
Appointment' field and then apply the formatting to that field

I understand that your saying i should locate the record first and then
apply
the formatting code that i know works however as the record is on a
different
form im not sure how to go about this, i.e. i guess 'Me.recordset' etc
wont
work?

Lee

BruceM wrote:
As Douglas said, the code you are trying is not the way to use
Bookmark. My
suggestion was to find the record. If the formatting is working now,
it
should work when you find the record. Do not attempt to find the
record and
apply formatting at the same time.

The sample code provides the syntax using an input box:

********************
Private Sub cmdFindContactName_Click()

Dim rst As DAO.Recordset
Dim strCriteria As String

strCriteria = "[ContactName] Like '*" & InputBox("Enter the " _
& "first few letters of the name to find") & "*'"

Set rst = Me.RecordsetClone
rst.FindFirst strCriteria
If rst.NoMatch Then
MsgBox "No entry found.", vbInformation
Else
Me.Bookmark = rst.Bookmark
End If

Set rst = Nothing

End Sub

********************
You can use another way to select the criteria by which you would find
the
record, but the only way I or anybody else can help is to know what
that is.
How are you determining which record to find? Text box? Combo box?

Bruce,

[quoted text clipped - 60 lines]
thanks
Lee
 
B

BruceM

A very thorough explanation, and a new word (octothorpe) too. Thank you for
the information about assuring no ambiguity in the date. I think I have not
always been as careful about that as I should be. Something else to keep an
eye on going forward, and something to look for if I am getting unexpected
results from date criteria on another computer where the date settings may
be different from mine.

Douglas J. Steele said:
I suppose you could say that it's because of the fact that strCriteria is a
string.

While internally dates are numbers (eight byte floating points, where the
integer portion represents the date as the number of days relative to 30
Dec, 1899 and the decimal portion represents the time as a fraction of a
day), if you concatenate a date value to a string, you'll going to get the
date written out as a date, using whatever Short Date format has been
defined through Regional Settings. If that format happens to be
dd/mm/yyyy, for 01 May, 2009 you'd get

[DateField] = 01/05/2009

When dealing with date values in Where clauses, they need to be delimited
with octothorpes (#), and the date must be formatted in a manner that
Access will recognize. Generally, that means mm/dd/yyyy format or
yyyy-mm-dd format, although other unambiguous formats could be used (dd
mmm yyyy). Even if you use

strCriteria = "[DateField] = #" & datYourVariable & "#"

so that you have

[DateField] = #01/05/2009#

it won't work. Regardless of the fact that the Short Date format is set to
dd/mm/yyyy, Access will treat that as 05 January, 2009.

That's the reason for using an explicit format.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


BruceM said:
Is that because strCriteria is a string variable, so the date variable
needs to be converted to a string value? If so, why that particular
formatting? I looked back through the thread, but could not find anything
specific about the date variable other than that it was global.

Douglas J. Steele said:
Not quite, Bruce.

strCriteria = "[DateField] = " & Format(datYourVariable,
"\#yyyy\-mm\-dd\#")


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


"BruceM" <bamoob_at_yawhodotcalm.not> wrote in message
You can use the date variable as the criteria:

Private Sub cmdFindContactName_Click()

Dim rst As DAO.Recordset
Dim strCriteria As String

strCriteria = "[DateField] = " & datYourVariable
Set rst = Me.RecordsetClone
rst.FindFirst strCriteria
If rst.NoMatch Then
MsgBox "No entry found.", vbInformation
Else
Me.Bookmark = rst.Bookmark
End If

Set rst = Nothing

End Sub

Are you trying to format a control on one form based on a value on
another form? Is there any connection between the data on one form and
the data on the other one? Please decribe in plain language, without
database code or terminology, exactly what you are trying to
accomplish.

Bruce,

the way in which i want to determine which record to find is by using
the
value stored in the global variable 'Dates2' this will be a date, i
then want
to pull up the record which has the matching date in the 'Date Of
Appointment' field and then apply the formatting to that field

I understand that your saying i should locate the record first and
then apply
the formatting code that i know works however as the record is on a
different
form im not sure how to go about this, i.e. i guess 'Me.recordset' etc
wont
work?

Lee

BruceM wrote:
As Douglas said, the code you are trying is not the way to use
Bookmark. My
suggestion was to find the record. If the formatting is working now,
it
should work when you find the record. Do not attempt to find the
record and
apply formatting at the same time.

The sample code provides the syntax using an input box:

********************
Private Sub cmdFindContactName_Click()

Dim rst As DAO.Recordset
Dim strCriteria As String

strCriteria = "[ContactName] Like '*" & InputBox("Enter the " _
& "first few letters of the name to find") & "*'"

Set rst = Me.RecordsetClone
rst.FindFirst strCriteria
If rst.NoMatch Then
MsgBox "No entry found.", vbInformation
Else
Me.Bookmark = rst.Bookmark
End If

Set rst = Nothing

End Sub

********************
You can use another way to select the criteria by which you would find
the
record, but the only way I or anybody else can help is to know what
that is.
How are you determining which record to find? Text box? Combo box?

Bruce,

[quoted text clipped - 60 lines]
thanks
Lee
 

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