assigning value to variable

Wysy

Registered User.
Local time
Yesterday, 17:25
Joined
Jul 5, 2015
Messages
335
Hi,
I am trying to shorten the code but i am not sure if it is possible at all, since error is created.
What i am trying to accomplish is to use a string that is repeated in several times while the values of the variables are passed from an other open form like this:
open form is: Forms!frm1
x variable value is assigned from the open form control y
x=Forms!frm1.y
Since there are several controls i need to use i think simplifying the coding like this:
s="Forms!frm1." and use it
x=s & y
It does not work. Am i completely wrong with the idea, or there is only a syntax error?
thanks
 
It works fine, but when i split it, it fails.
The Control name is the one that changes, so what i would like is something like this:
s=Forms("Your form name").Controls & controlname whereas
controlname=Your Control name1
controlname=Your Control name2
controlname=Your Control name3
 
I got it working
s=Forms("Your form name").Controls(controlname)
controlname="Your Control name1"
controlname="Your Control name2"
controlname="Your Control name3"


Thanks for the help!
 
Code:
Dim controlname$, Var01, Var02, Var03
controlname="Your Control name1"
Var01 = Forms("Your form name").Controls(controlname)
controlname="Your Control name2"
Var02 = Forms("Your form name").Controls(controlname)
controlname="Your Control name3"
Var03 = Forms("Your form name").Controls(controlname)
 
Not quite...
ss=s & controlname
This does not work.
 
is you variable Public/Global?

s= s & Me.Name & "." & theControlName
 
no it is not.
May be to ask differently:
how to avoid to retype every time the part of the code that is repeated.
s=Forms!frm1.controlname
keep Forms!frm1 and let the controlname change like this
s=Forms!frm1
ss=s & controlname1
ss=s & controlname2
etc
 
how to avoid to retype every time the part of the code that is repeated.
Code:
Dim sFormName$, sControlName$
    sFormName = "Test"
    With Forms(sFormName)
       
        sControlName = "txtStartDate"
        Debug.Print sControlName & " : " & .Controls(sControlName).Value
       
        sControlName = "txtEndDate"
        Debug.Print sControlName & " : " & .Controls(sControlName).Value
   
    End With
 
Last edited:
Thank you. If there is no shorter way, i am going to use this. Works perfectly
thanks
 
If there is no shorter way,
there is more :
Code:
Dim sFormName$, sControlName$
Dim frm As Form

    sFormName = "Test"
    Set frm = Forms(sFormName)
        
    sControlName = "txtStartDate"
    Debug.Print sControlName & " value is: " & frm.Controls(sControlName).Value
        
    sControlName = "txtEndDate"
    Debug.Print sControlName & " value is: " & frm.Controls(sControlName).Value
    
    Set frm = Nothinng
 
Great! And how does it work with subform control?
 
by the first or second method you have suggested?
With... or the Set?
 
And for the first method? Sorry for it but i would really like to understand the syntax.
 
Code:
Dim sFormName$, sControlName$
Dim frm As Form

    sFormName = "Test"
    With Forms(sFormName)

        sControlName = "objSubForm" 'Your subform control name

        Debug.Print "In your subform control you are using form : " & _
              .Controls(sControlName).Form.Name
        'or
        Debug.Print "In your subform control you are using form : " & _
              .Controls(sControlName).SourceObject
    End With
 
I wrote this but does not work

Code:
With Forms(frmMassInput)
    txPartner = .Controls(fsubMassInput2).Form.CustomerID
    txAmount = .Controls(fsubMassInput2).Form.Total
    txCurrency = .Controls(fsubMassInput2).Form.currencyX
    txDescription = .Controls(fsubMassInput2).Form.TreatmentID
End With
 
IMHO Is Better so
Code:
With Forms(frmMassInput).Controls(fsubMassInput2).Form
    txPartner = .CustomerID
    txAmount = .Total
    txCurrency = .currencyX
    txDescription = .TreatmentID
End With
 

Users who are viewing this thread

Back
Top Bottom