storing a string in a bound field

T

TimH

I have a function that generates a string and I need to enter it in A Bound
Field:
Public Function PtKey()
....
....
strTemp = PtKey
where PtKey is the bound field;
On Debug.Print the value is printed in Immediate window however I am not
able to populate the Bound Field Outomaticaly

Please Help

As Always Thank you for your assistance.
 
B

BruceM

I assume you mean "bound control". A control can be bound to a field, but a
field itself is neither bound nor unbound.
PtKey is the name of both the function and the field (or control)? That's
almost certainly at least part of the problem.
Your code fragment suggests you are setting strTemp to the value produced by
the PtKey function. To set a field value you need to turn it around.
Assuming a field of a different name than the function name:
Me.FieldName = PtKey
 
T

TimH

Thank you Bruce, Part of the problem with a newBee is that even the questions
are a challange,
The ModPatientKey has a Function PtKey (I learned from a previous error
both could not have the same name) and my form has a controll named Key
bound to a Table with the field Key and I would like to outopopulate this
field with the result of the function which produces the :

strTemp = PtKey

this function works as I am able to debug.Print PtKey but not
outopopulate.thaks any help is appreciated
 
B

BruceM

First of all, Key is a reserved word in Jet. Reserved words should not be
used for names of fields, controls, objects, or anything else. For more
information, see:
http://www.accessmvp.com/JConrad/accessjunkie/resources.html#ReservedWords
and
http://allenbrowne.com/AppIssueBadWord.html
I would use a different name for controls than are used for fields, too.

That being said, this should work, as I suggested in my first response (but
with a made-up field name):
Me.Key = PtKey
or
Me.Key = strTemp

The other way around you are setting the value of the variable to the
field's (or control's) value.
 
T

TimH

Ok BruceM, First I thank you for responding and for the suggestion on Key
words, Had I know I would have used another word an will chane it if I can
without messing the Db., I tried a your suggestion without any luck, I'm
still pluging on; here is the whole function:

Public Function PtKey()
Dim strTmp As String
Dim dtVisitDate As Date
Dim strPtKey As String
Dim strLastName As String

strTmp = Me.Visit_Date & ""

If IsDate(strTmp) Then ' from John Spencer
dtVisitDate = DateValue(strTmp) ' from J.S.
strTmp = Format(dtVisitDate, "mmddyyyy") 'yyyymmdd--From J.S.
End If

Build_VisitDate = strTmp
strTmp = strTmp & "-"
strSSN = Me.SSN & ""
' OK so it works, I used the code below to take a full SSN
' and convert it to the last 4
strSSN = Right(strSSN, 4)
strTmp = strTmp & strSSN
strLastName = Me.LastName & ""
strLastName = Left(strLastName, 1)
strTmp = strTmp & strLastName

End Function


Now on my tblPATIENTS I have the field (unfortunantly named ) Key and on the
form Patients the controll(text) Key where I was manualy entring the " key"

I hope this clearefies my question.
 
B

BruceM

Add a msgbox or depug.print to the end of the code. Is strTmp what it
should be? By the way, are the empty strings ("") to avoid a null if there
is nothing in the field?
If strTmp is as you would expect, what do you want to do with it? As Vandal
asked, how are you calling the function? Where is the function located - in
a separate module, or in the form's code module? In what way are you having
no luck?

AccessVandal via AccessMonster.com said:
If you are using this function from a Standard Module, you cannot use “Meâ€.

Me refer to the current object in the form’s module. If you place the
function into the form’s module than you can use “Meâ€.

I’m surprised that your VB editor did not give any error. Did you include
“Option Explicit�

You might want to reconsider your function to include argument like…

Public Function PtKey(strTmp As Date, strSSN As String) As String

Or

Just move the function into the form’s module and delete the function from
ModPatientKey.

Ok BruceM, First I thank you for responding and for the suggestion on Key
words, Had I know I would have used another word an will chane it if I can
without messing the Db., I tried a your suggestion without any luck, I'm
still pluging on; here is the whole function:

Public Function PtKey()
Dim strTmp As String
Dim dtVisitDate As Date
Dim strPtKey As String
Dim strLastName As String

strTmp = Me.Visit_Date & ""

If IsDate(strTmp) Then ' from John Spencer
dtVisitDate = DateValue(strTmp) ' from J.S.
strTmp = Format(dtVisitDate, "mmddyyyy") 'yyyymmdd--From J.S.
End If

Build_VisitDate = strTmp
strTmp = strTmp & "-"
strSSN = Me.SSN & ""
' OK so it works, I used the code below to take a full SSN
' and convert it to the last 4
strSSN = Right(strSSN, 4)
strTmp = strTmp & strSSN
strLastName = Me.LastName & ""
strLastName = Left(strLastName, 1)
strTmp = strTmp & strLastName

End Function

Now on my tblPATIENTS I have the field (unfortunantly named ) Key and on
the
form Patients the controll(text) Key where I was manualy entring the "
key"

I hope this clearefies my question.
I have a function that generates a string and I need to enter it in A
Bound
Field:
[quoted text clipped - 9 lines]
As Always Thank you for your assistance.
 
T

TimH

BruceM
Thanks, your suggestion:public Function PtKey(strTmp As Date, strSSN As
String) As String
worked. I had to place the code in the Form itself for it to work as a sub
on Private Sub PtKey_GotFocus()

to answer your other questions
yes Option Explicit was on
No other Errors
Thank you.
--
timH


BruceM said:
Add a msgbox or depug.print to the end of the code. Is strTmp what it
should be? By the way, are the empty strings ("") to avoid a null if there
is nothing in the field?
If strTmp is as you would expect, what do you want to do with it? As Vandal
asked, how are you calling the function? Where is the function located - in
a separate module, or in the form's code module? In what way are you having
no luck?

AccessVandal via AccessMonster.com said:
If you are using this function from a Standard Module, you cannot use “Meâ€.

Me refer to the current object in the form’s module. If you place the
function into the form’s module than you can use “Meâ€.

I’m surprised that your VB editor did not give any error. Did you include
“Option Explicit�

You might want to reconsider your function to include argument like…

Public Function PtKey(strTmp As Date, strSSN As String) As String

Or

Just move the function into the form’s module and delete the function from
ModPatientKey.

Ok BruceM, First I thank you for responding and for the suggestion on Key
words, Had I know I would have used another word an will chane it if I can
without messing the Db., I tried a your suggestion without any luck, I'm
still pluging on; here is the whole function:

Public Function PtKey()
Dim strTmp As String
Dim dtVisitDate As Date
Dim strPtKey As String
Dim strLastName As String

strTmp = Me.Visit_Date & ""

If IsDate(strTmp) Then ' from John Spencer
dtVisitDate = DateValue(strTmp) ' from J.S.
strTmp = Format(dtVisitDate, "mmddyyyy") 'yyyymmdd--From J.S.
End If

Build_VisitDate = strTmp
strTmp = strTmp & "-"
strSSN = Me.SSN & ""
' OK so it works, I used the code below to take a full SSN
' and convert it to the last 4
strSSN = Right(strSSN, 4)
strTmp = strTmp & strSSN
strLastName = Me.LastName & ""
strLastName = Left(strLastName, 1)
strTmp = strTmp & strLastName

End Function

Now on my tblPATIENTS I have the field (unfortunantly named ) Key and on
the
form Patients the controll(text) Key where I was manualy entring the "
key"

I hope this clearefies my question.
I have a function that generates a string and I need to enter it in A
Bound
Field:
[quoted text clipped - 9 lines]

As Always Thank you for your assistance.
 
B

BruceM

I should repeat that it's not a good idea to have a field and a function
with the same name.

The reason the code as it stands needs to be in the form's module is that
Me.SomeField refers to a field that is available to a form. If the code is
in a standard module you can't use Me.SomeField, as Vandal pointed out,
because the module is not pinned to a form, so has no idea what to make of
Me.SomeField.
There are ways around that. However, unless you are using the same code for
several forms or reports based on the same recordset you may as well keep
the code in the form's code module.

By the way, to give credit where it's due, Vandal suggested including the
string arguments in the function. Option explicit is a good idea, but even
without it I think the compiler would have had a problem with Me.SomeField.
Did you compile the code (Debug >> Compile)?

Anyhow, glad to hear you got it working.

TimH said:
BruceM
Thanks, your suggestion:public Function PtKey(strTmp As Date, strSSN As
String) As String
worked. I had to place the code in the Form itself for it to work as a sub
on Private Sub PtKey_GotFocus()

to answer your other questions
yes Option Explicit was on
No other Errors
Thank you.
--
timH


BruceM said:
Add a msgbox or depug.print to the end of the code. Is strTmp what it
should be? By the way, are the empty strings ("") to avoid a null if
there
is nothing in the field?
If strTmp is as you would expect, what do you want to do with it? As
Vandal
asked, how are you calling the function? Where is the function located -
in
a separate module, or in the form's code module? In what way are you
having
no luck?

AccessVandal via AccessMonster.com said:
If you are using this function from a Standard Module, you cannot use
“Meâ€.

Me refer to the current object in the form’s module. If you place the
function into the form’s module than you can use “Meâ€.

I’m surprised that your VB editor did not give any error. Did you
include
“Option Explicit�

You might want to reconsider your function to include argument like…

Public Function PtKey(strTmp As Date, strSSN As String) As String

Or

Just move the function into the form’s module and delete the function
from
ModPatientKey.


TimH wrote:
Ok BruceM, First I thank you for responding and for the suggestion on
Key
words, Had I know I would have used another word an will chane it if I
can
without messing the Db., I tried a your suggestion without any luck,
I'm
still pluging on; here is the whole function:

Public Function PtKey()
Dim strTmp As String
Dim dtVisitDate As Date
Dim strPtKey As String
Dim strLastName As String

strTmp = Me.Visit_Date & ""

If IsDate(strTmp) Then ' from John Spencer
dtVisitDate = DateValue(strTmp) ' from J.S.
strTmp = Format(dtVisitDate, "mmddyyyy") 'yyyymmdd--From J.S.
End If

Build_VisitDate = strTmp
strTmp = strTmp & "-"
strSSN = Me.SSN & ""
' OK so it works, I used the code below to take a full SSN
' and convert it to the last 4
strSSN = Right(strSSN, 4)
strTmp = strTmp & strSSN
strLastName = Me.LastName & ""
strLastName = Left(strLastName, 1)
strTmp = strTmp & strLastName

End Function

Now on my tblPATIENTS I have the field (unfortunantly named ) Key and
on
the
form Patients the controll(text) Key where I was manualy entring the "
key"

I hope this clearefies my question.
I have a function that generates a string and I need to enter it in A
Bound
Field:
[quoted text clipped - 9 lines]

As Always Thank you for your assistance.
 
T

TimH

Thank you BruceM' and Vandal
I moved the code in the form, and attached it to the Private sub
Pt_Key_GotFocus()
and the code worked.

I now understand what you mean about a function in public In a Standart
Module would have no way of linking to a form //or synchronising since there
is no original record/field to synch. with.
--
timH


BruceM said:
I should repeat that it's not a good idea to have a field and a function
with the same name.

The reason the code as it stands needs to be in the form's module is that
Me.SomeField refers to a field that is available to a form. If the code is
in a standard module you can't use Me.SomeField, as Vandal pointed out,
because the module is not pinned to a form, so has no idea what to make of
Me.SomeField.
There are ways around that. However, unless you are using the same code for
several forms or reports based on the same recordset you may as well keep
the code in the form's code module.

By the way, to give credit where it's due, Vandal suggested including the
string arguments in the function. Option explicit is a good idea, but even
without it I think the compiler would have had a problem with Me.SomeField.
Did you compile the code (Debug >> Compile)?

Anyhow, glad to hear you got it working.

TimH said:
BruceM
Thanks, your suggestion:public Function PtKey(strTmp As Date, strSSN As
String) As String
worked. I had to place the code in the Form itself for it to work as a sub
on Private Sub PtKey_GotFocus()

to answer your other questions
yes Option Explicit was on
No other Errors
Thank you.
--
timH


BruceM said:
Add a msgbox or depug.print to the end of the code. Is strTmp what it
should be? By the way, are the empty strings ("") to avoid a null if
there
is nothing in the field?
If strTmp is as you would expect, what do you want to do with it? As
Vandal
asked, how are you calling the function? Where is the function located -
in
a separate module, or in the form's code module? In what way are you
having
no luck?

If you are using this function from a Standard Module, you cannot use
“Meâ€.

Me refer to the current object in the form’s module. If you place the
function into the form’s module than you can use “Meâ€.

I’m surprised that your VB editor did not give any error. Did you
include
“Option Explicit�

You might want to reconsider your function to include argument like…

Public Function PtKey(strTmp As Date, strSSN As String) As String

Or

Just move the function into the form’s module and delete the function
from
ModPatientKey.


TimH wrote:
Ok BruceM, First I thank you for responding and for the suggestion on
Key
words, Had I know I would have used another word an will chane it if I
can
without messing the Db., I tried a your suggestion without any luck,
I'm
still pluging on; here is the whole function:

Public Function PtKey()
Dim strTmp As String
Dim dtVisitDate As Date
Dim strPtKey As String
Dim strLastName As String

strTmp = Me.Visit_Date & ""

If IsDate(strTmp) Then ' from John Spencer
dtVisitDate = DateValue(strTmp) ' from J.S.
strTmp = Format(dtVisitDate, "mmddyyyy") 'yyyymmdd--From J.S.
End If

Build_VisitDate = strTmp
strTmp = strTmp & "-"
strSSN = Me.SSN & ""
' OK so it works, I used the code below to take a full SSN
' and convert it to the last 4
strSSN = Right(strSSN, 4)
strTmp = strTmp & strSSN
strLastName = Me.LastName & ""
strLastName = Left(strLastName, 1)
strTmp = strTmp & strLastName

End Function

Now on my tblPATIENTS I have the field (unfortunantly named ) Key and
on
the
form Patients the controll(text) Key where I was manualy entring the "
key"

I hope this clearefies my question.
I have a function that generates a string and I need to enter it in A
Bound
Field:
[quoted text clipped - 9 lines]

As Always Thank you for your assistance.
 

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