Go Back   Access World Forums > Microsoft Access Discussion > Modules & VBA

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 12-13-2016, 06:27 AM   #1
selvsagt
Newly Registered User
 
Join Date: Jun 2006
Posts: 99
Thanks: 20
Thanked 1 Time in 1 Post
selvsagt is an unknown quantity at this point
Use control name and control path in string

Hi.

I am filling out a PDF based on values in my form.
The form has 5 subforms, with a lot of controls on each.

The code works today, but it takes to much time to change.
The idea is that i reference each field in the form (each textbox control) by its name, and then i name the pdf field the same.

So the bottom half of the code below today lookes like this, and it fills out the pdf with customer information. Beneath I have one field per subform as an example. I would have maxed out the number of controls if i kept them in 1 form, therefore I have 5 subforms on my main form. The "button" is launched from the parent form.


First: this code works great today... but is difficult to maintain if the form changes, therefore i would like to improve my code.
Code:
Print #intFile, "<field name=""accountID"">"
Print #intFile, "<value>" & Me!account.Form!accountID & "</value>"
Print #intFile, "</field>"

Print #intFile, "<field name=""selskapstype"">"
Print #intFile, "<value>" & Me!HVV.Form!selskapstype & "</value>"
Print #intFile, "</field>"

Print #intFile, "<field name=""InnsattBel"">"
Print #intFile, "<value>" & Me!idok.Form!InnsattBel & "</value>"
Print #intFile, "</field>"

Print #intFile, "<field name=""etTekst"">"
Print #intFile, "<value>" & Me!et.Form!etTekst & "</value>"
Print #intFile, "</field>"

Print #intFile, "<field name=""FO_tekst"">"
Print #intFile, "<value>" & Me!FOtekst.Form!FO_tekst & "</value>"
Print #intFile, "</field>"

'to close up it adds this at the bottom to open the pdf
Print #intFile, "</fields>"
Print #intFile, "</xfdf>"
Close #intFile
ShellEx strOutput
Exit Sub
Instead of naming each and every control to the same name in the pdf, I am trying to make the program understand that i would like it to do the same for EVERY control the same way, without me typing in everything.

Code:
Dim ctl_input As Access.Control
Dim ctl_ref As Access.Reference

'starts the loop
For Each ctl_input In Me.Controls
            Print #intFile, "<field name=" & ctl_input.Name & ">"
            Print #intFile, "<value>" & ctl_ref & "</value>"
            Print #intFile, "</field>"
Next ctl_input
'continues until there is no more controls then adds the finishing lines

Print #intFile, "</fields>"
Print #intFile, "</xfdf>"
Close #intFile
ShellEx strOutput
Exit Sub
It fails here:
Code:
Print #intFile, "<value>" & ctl_ref & "</value>"
with the error message: Object variable or with block variable not set, run-time error 91.
I guess I am not getting the "path" to the control correctly. the path/ control address should probably be converted to a string... anyone knows how to do this?

Anyone?

__________________
Working on a Citrix Client using MS Access 2010 as FrontEnd and MSSql server Express 2014 as backend
selvsagt is offline   Reply With Quote
Old 12-13-2016, 08:32 AM   #2
pbaldy
Wino Moderator
 
pbaldy's Avatar
 
Join Date: Aug 2003
Location: Nevada, USA
Posts: 33,296
Thanks: 13
Thanked 4,112 Times in 4,044 Posts
pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold
Re: Use control name and control path in string

I suspect you want

ctl_input.Value

Which should return the value held in the control.
__________________
Paul
Microsoft Access MVP 2007-2019

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
pbaldy is offline   Reply With Quote
The Following User Says Thank You to pbaldy For This Useful Post:
selvsagt (12-13-2016)
Old 12-13-2016, 01:01 PM   #3
selvsagt
Newly Registered User
 
Join Date: Jun 2006
Posts: 99
Thanks: 20
Thanked 1 Time in 1 Post
selvsagt is an unknown quantity at this point
Re: Use control name and control path in string

Quote:
Originally Posted by pbaldy View Post
I suspect you want

ctl_input.Value

Which should return the value held in the control.
Thank you very much, you are absolutely right!

It gave me a new error in the adobe file itself (xml parsing error: not well-formed (invalid token) (error code 4), line 5 column 13 of file).

The reason for this "token error" was how I used quotes. So when I changed it;
Code:
Print #intFile, "<field name=" & "'" & ctl_input.Name & "'" & ">"
It now works! Thank you!

__________________
Working on a Citrix Client using MS Access 2010 as FrontEnd and MSSql server Express 2014 as backend

Last edited by selvsagt; 12-13-2016 at 01:16 PM.
selvsagt is offline   Reply With Quote
Old 12-13-2016, 01:45 PM   #4
pbaldy
Wino Moderator
 
pbaldy's Avatar
 
Join Date: Aug 2003
Location: Nevada, USA
Posts: 33,296
Thanks: 13
Thanked 4,112 Times in 4,044 Posts
pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold
Re: Use control name and control path in string

Happy to help!
__________________
Paul
Microsoft Access MVP 2007-2019

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
pbaldy is offline   Reply With Quote
Old 12-13-2016, 01:58 PM   #5
selvsagt
Newly Registered User
 
Join Date: Jun 2006
Posts: 99
Thanks: 20
Thanked 1 Time in 1 Post
selvsagt is an unknown quantity at this point
Re: Use control name and control path in string

And a new problem occurred..which probably is an easy one.
The button that calls this is on the main form.
The actual data is in 5 different subforms.
The code worked when i called it from the form that actually contains the control, but I dont figure out how to deal with the controls on the subform.

I think I am looking for something like;
if control is subform then subformname.ctl_input.name
and subformname.ctl_input.value

But this doesn't solve the naming of the actual control nor the value in that control...

Is it possible to add a "for each" within the other to handle these differently and how would i reference to the subformname.controlname.value?


Here's what I got, ??? is probably where my issues start:
Code:
Dim ctl_input As Control

For Each ctl_input In Me.Controls
    If TypeOf ctl_input Is TextBox Then
        
        If IsNull(ctl_input) = False Then   'remove all IsNull's
                Print #intFile, "<field name=" & "'" & ctl_input.Name & "'" & ">"
                Print #intFile, "<value>" & ctl_input.Value & "</value>"
                Print #intFile, "</field>"
                    
                
        End If
                
                  
                    If ctl_input.ControlType = acSubform Then
                            Print #intFile, "<field name=" & "'" & ???.ctl_input.Name & "'" & ">"
                            Print #intFile, "<value>" & ???.ctl_input.Value & "</value>"
                            Print #intFile, "</field>"
                               
                    End If
    End If
    Next ctl_input
__________________
Working on a Citrix Client using MS Access 2010 as FrontEnd and MSSql server Express 2014 as backend
selvsagt is offline   Reply With Quote
Old 12-13-2016, 02:15 PM   #6
pbaldy
Wino Moderator
 
pbaldy's Avatar
 
Join Date: Aug 2003
Location: Nevada, USA
Posts: 33,296
Thanks: 13
Thanked 4,112 Times in 4,044 Posts
pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold
Re: Use control name and control path in string

Untried, but I think you'd have to have another loop within that section, to loop the controls within the subform. You'd need a separate variable for that loop I suspect. If you have trouble, posting a sample db we can play with would make it easier to help. Off the top of my head, something like:

For Each ctl_input2 In Me(ctl_input.Name).Controls

but again, untested.
__________________
Paul
Microsoft Access MVP 2007-2019

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
pbaldy is offline   Reply With Quote
Old 12-14-2016, 03:32 AM   #7
selvsagt
Newly Registered User
 
Join Date: Jun 2006
Posts: 99
Thanks: 20
Thanked 1 Time in 1 Post
selvsagt is an unknown quantity at this point
Re: Use control name and control path in string

Quote:
Originally Posted by pbaldy View Post
Untried, but I think you'd have to have another loop within that section, to loop the controls within the subform. You'd need a separate variable for that loop I suspect. If you have trouble, posting a sample db we can play with would make it easier to help. Off the top of my head, something like:

For Each ctl_input2 In Me(ctl_input.Name).Controls

but again, untested.
I tried this approach without any luck. This is absolutely doable, but i end up knocking med head against the wall in the naming of the secondary ctl (the one's one the subs).

So, for a quick and dirty fix I ended up doing the same loop 5 times for alle the different subs.



Code:
For Each ctl_input In Me.account.Controls
    If TypeOf ctl_input Is TextBox Then
        If IsNull(ctl_input) = False Then   'remove all IsNull's
                Print #intFile, "<field name=" & "'" & ctl_input.Name & "'" & ">"
                Print #intFile, "<value>" & ctl_input.Value & "</value>"
                Print #intFile, "</field>"
                Debug.Print ctl_input.Name & ": " & ctl_input.Value
        End If
    End If
Next ctl_input
   
For Each ctl_input In Me.HVV.Controls
    If TypeOf ctl_input Is TextBox Then
        If IsNull(ctl_input) = False Then   'remove all IsNull's
                Print #intFile, "<field name=" & "'" & ctl_input.Name & "'" & ">"
                Print #intFile, "<value>" & ctl_input.Value & "</value>"
                Print #intFile, "</field>"
                Debug.Print ctl_input.Name & ": " & ctl_input.Value
        End If
    End If
Next ctl_input

and so on....

__________________
Working on a Citrix Client using MS Access 2010 as FrontEnd and MSSql server Express 2014 as backend
selvsagt is offline   Reply With Quote
Old 12-14-2016, 07:45 AM   #8
pbaldy
Wino Moderator
 
pbaldy's Avatar
 
Join Date: Aug 2003
Location: Nevada, USA
Posts: 33,296
Thanks: 13
Thanked 4,112 Times in 4,044 Posts
pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold
Re: Use control name and control path in string

Glad you got it working.

__________________
Paul
Microsoft Access MVP 2007-2019

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
pbaldy is offline   Reply With Quote
Reply

Tags
for each ctl , pdf , vba

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Attachment Control - Path fenhow Forms 1 01-21-2016 08:00 PM
Path to Subform Control sladetroityer Forms 15 12-02-2011 04:42 AM
Can the default path be set for the Attachment control neilhoop General 4 06-28-2009 03:04 PM
VBA Path for variable name of label control Dudley Modules & VBA 2 06-01-2005 03:05 AM
File control and path handling Tim L General 5 10-24-2003 09:26 AM




All times are GMT -8. The time now is 07:08 AM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World