Update a variable field from a text box value

S

skynugget

Hello all, im a little stumped with some VBA sytax.

to start off, i have a field that is loaded with 3 text boxes

2 of the text boxes are populated on the form load with data i want to
use
txtFormName (ex. frmTest)
txtFieldName (ex. txtTest)

the 3rd text box is a value that i want to send to a field based on
the first 2 text boxes.
txtValue ex. test

im trying to do it with this click statment

Private Sub Command11_Click()

Forms!Me.txtForm.Me.txtField.Value = Me.txtValue
Forms!Me.txtForm.Refresh

End Sub

when i click my button, i get an error that it cant find the form "
Me.txtForm ". So i guess the value isn't getting passed

its executing

Forms!Me.txtForm.Me.txtField.Value = test

but it should be trying to execute

Forms!frmTest.txtTest.Value = test

in this example if i could get it to work.

thanks so much!
 
D

Damon Heron

How about:

Private Sub Command11_Click()
Me.txtValue = Me.txtField
End Sub

Is txtValue bound to a field in a table?? If so, this would update the
table field with the value
of the textbox "txtField".... I guess that is what you want.
Damon
 
K

Ken Sheridan

I'm not sure I fully understand what your aim is here but if you are
attempting to assign a value from a control txtValue on the current
form to a control whose name is in the txtField control on the current
form, and which is on an open form whose name is in the control
txtForm on the current form, then this should do it:

Forms(Me.txtForm).Controls(Me.txtField) = Me.txtValue

You should not have to refresh the control.

Ken Sheridan
Stafford, England
 
S

stefy

Hello all, im a little stumped with some VBA sytax.

to start off, i have a field that is loaded with 3 text boxes

2 of the text boxes are populated on the form load with data i want to
use
txtFormName (ex. frmTest)
txtFieldName (ex. txtTest)

the 3rd text box is a value that i want to send to a field based on
the first 2 text boxes.
txtValue ex. test

im trying to do it with this click statment

Private Sub Command11_Click()

Forms!Me.txtForm.Me.txtField.Value = Me.txtValue
Forms!Me.txtForm.Refresh

End Sub

when i click my button, i get an error that it cant find the form "
Me.txtForm ". So i guess the value isn't getting passed

its executing

Forms!Me.txtForm.Me.txtField.Value = test

but it should be trying to execute

Forms!frmTest.txtTest.Value = test

in this example if i could get it to work.

thanks so much!
 
S

skynugget

I'm not sure I fully understand what your aim is here but if you are
attempting to assign a value from a control txtValue on the current
form to a control whose name is in the txtField control on the current
form, and which is on an open form whose name is in the control
txtForm on the current form, then this should do it:

Forms(Me.txtForm).Controls(Me.txtField) = Me.txtValue

You should not have to refresh the control.

Ken Sheridan
Stafford, England

Thanks ken, you nailed it on the head, sorry for my poor description.
but now i get the error, that it cant find the form that exists...
could it be becuase the form is already open or something?
 
J

Jack Cannon

Ken's suggestion is certainly valid. But I believe the command requires a
minor modification based on what I read in your posting.

Instead of:
Forms(Me.txtForm).Controls(Me.txtField) = Me.txtValue

It probably should be:
Forms(Me.txtFormName).Controls(Me.txtFieldName) = Me.txtValue

However the real issue here is whether all possible forms are loaded which
doesn't seem likely.

You probably need to check to see if txtFormName IsLoaded.
If not, you need to click on a button to open txtFormName then click on the
button to execute the command to update txtFieldName.
After the form is updated you may need to close it and that may require a
third button to close txtFormName.

Jack Cannon
 
S

skynugget

Ken's suggestion is certainly valid.  But I believe the command requires a
minor modification based on what I read in your posting.

Instead of:
Forms(Me.txtForm).Controls(Me.txtField) = Me.txtValue

It probably should be:
Forms(Me.txtFormName).Controls(Me.txtFieldName) = Me.txtValue

However the real issue here is whether all possible forms are loaded which
doesn't seem likely.

You probably need to check to see if txtFormName IsLoaded.
If not, you need to click on a button to open txtFormName then click on the
button to execute the command to update txtFieldName.
After the form is updated you may need to close it and that may require a
third button to close txtFormName.

Jack Cannon

well honestly maybe im going about this all wrong to begin with.

basically im launching frmKeyPad from a textbox in sfrmRegister. on
exit of frmKeyPad i want the value of txtValue to be propigated the
sfrmRegister.textbox from wich it was launched. the text boxes coulf
be txtPrice txtQty etc.... does that make sence?
 
K

Ken Sheridan

By the look of its name sfrmRegister is a subform. Subforms are not
included in the Forms collection so you cannot reference it by its
name. Its referenced by the Form property of the parent form, so the
syntax to reference a control on a subform is:

Forms(<parentform>).<subformcontrol>.Form.<controlname>

You can pass the name of the control which 'launches' the frmKeyPad
form to it as its OpenArgs property. You don't say how it is
'launched' by a control, but assuming the control is the current
control then code in the subform's module to open frmKeyPad would be:

Dim strControl as String

strControl = Me.ActiveControl.Name
DoCmd.OpenForm "frmKeyPad", OpenArgs:=strControl

You can then set the value of the control in the Close event procedure
of frmKeyPad with:

Dim frm as Form
Dim ctrl as Control

If Not IsNull(Me.OpenArgs) Then
Set frm = Forms("YourParentForm").YourSubformControl.Form
Set ctrl = frm.Controls(Me.OpenArgs)
ctrl = Me.txtValue
End If

where YourParentForm is the name of the parent form containing the
sfrmRegister subform, and YourSubformControl is the name of the
subform control in the parent form's Controls collection which houses
the subform. This might or might not be the same name as its source
form object. Its not clear from your post whether sfrmRegister is the
name of the subform control, its source form object or both.

If frmKeyPad is used by more than one form you can pass multiple named
arguments as the OpenArgs property, enabling you to pass the parent
form, subform control and control names. You'll find a demo of a
module for doing this at:

http://community.netscape.com/n/pfx...yMessages&tsn=1&tid=24091&webtag=ws-msdevapps

Ken Sheridan
Stafford, England
 
J

Jack Cannon

Okay, I will give this a try.

When you open frmKeyPad specify "acDialog". That will immediately stop all
subsequent code within sfrmRegister from executing until frmKeyPad closes.
Now as I understand the objective, a value of txtValue is created on
frmKeyPad. Store that value in a global variable before frmKeyPad closes.
You can do this within the Form_Unload or Form_Close events or if you have a
Close button you can also do it on the On_Click event.

After frmKeyPad closes the code within sfrmRegister will resume. You can
then use the contents of the global variable to load sfrmRegister.textbox or
whatever else you wish to do with the variable.

Hope this helps.

Jack Cannon
 
S

skynugget

Okay, I will give this a try.

When you open frmKeyPad specify "acDialog".  That will immediately stopall
subsequent code within sfrmRegister from executing until frmKeyPad closes..  
Now as I understand the objective, a value of txtValue is created on
frmKeyPad.  Store that value in a global variable before frmKeyPad closes.  
You can do this within the Form_Unload or Form_Close events or if you have a
Close button you can also do it on the On_Click event.

After frmKeyPad closes the code within sfrmRegister will resume.  You can
then use the contents of the global variable to load sfrmRegister.textboxor
whatever else you wish to do with the variable.

Hope this helps.

Jack Cannon

thanks ken and john, got my crap working, and i learned a bit from
both of your posts.
 
S

stu

thanks ken and john, got my crap working, and i learned a bit from
both of your posts.

hey all, im back at it again... arg!

just to try to calrify. I have a form. frmRegister with the subform
sfrmRegister. The form frmKeypad is Launched via an onclick event on
a couple different txtboxes. at lunch i set some parameters with
openargs, then i use those to point back to the field i just clicked
on.

you guys see anything wrong with this code? it runs on a "close"
button on frmKeypad. Its goal is to take the value of TxtValue, and
put it in the field that originally "launched" it.


Public Sub Command11_Click()

Dim frm As Form
Dim ctrl As Control
Dim myarr() As String
Dim strform As String


myarr = Split(Me.OpenArgs, "|")
strControl = myarr(0)

strform = "Form_" & myarr(1)

Debug.Print strform

Set frm = strform 'Form_sfrmRegister
Set ctrl = frm.Controls(strControl)
ctrl = Me.txtValue


Form_sfrmRegister.Refresh
Form_frmRegister.Refresh
DoCmd.Close acForm, "frmKeyPad"

End Sub


my problem is in the "Set frm" line if i get rid of strfrom and
uncomment From_sfrmRegister it works flawlessly.

if i try to us strForm in that line, i get an error that access cant
find the form sfrmRegister. i could jsut leave the variable out of
set form, but i would like to luanch the keypad from another form in
the future.

i don't get it!
 
K

Ken Sheridan

You are using the class name to reference the subform, but ctrl being
an object variable expects a reference to the object per se not its
name, which is why it works when you reference the class directly.
You can only pass a string as the OpenArgs argument, however, not an
object, so try referencing the control on the subform like this:

Set ctrl = _
Forms(strParentForm). _
Controls(strSubFormControl). _
Form.Controls(strControl)

You'll need to pass the name of the parent form, the name of its
subform control and the name of the control to the frmKeyPad form via
the OpenArgs mechanism, which you can do as a delimited string and
then parse it into an array with the Split function as you are doing,
or you can use the module to which I referred you which enables you to
pass named or numbered arguments.

Ken Sheridan
Stafford, England
 
S

skynugget

You are using the class name to reference the subform, but ctrl being
an object variable expects a reference to the object per se not its
name, which is why it works when you reference the class directly.
You can only pass a string as the OpenArgs argument, however, not an
object, so try referencing the control on the subform like this:

Set ctrl =  _
     Forms(strParentForm). _
     Controls(strSubFormControl). _
     Form.Controls(strControl)

You'll need to pass the name of the parent form, the name of its
subform control and the name of the control to the frmKeyPad form via
the OpenArgs mechanism, which you can do as a delimited string and
then parse it into an array with the Split function as you are doing,
or you can use the module to which I referred you which enables you to
pass named or numbered arguments.

Ken Sheridan
Stafford, England

thanks again ken... im using this code to set my openargs:
Private Sub txtItemPrice_Click()
strControl = Me.ActiveControl.Name
strform = Me.Form.Name

DoCmd.OpenForm "frmKeyPad", OpenArgs:=strControl & "|" & strform
End Sub

is there a vba function to easily get the active parent form name?
 

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