We help IT Professionals succeed at work.

MS Access VBA How to export string to a text document within VBA

Dustin Stanley
on
293 Views
Last Modified: 2017-04-01
If I have a Long String of say code 3000 characters or more longer how can I send that to a text document in VBA. The string code will actually be in the VBA module.

Code = "uahfiuaheuifgha;vghauivgh;iuehgfehfiaedghewgh" _
& "iuasfiuasfhasoieiedoiuesdsglueildsugilsudgileglisurlvgusli" _
etc........

About 6 lines on Contionous Code.

I just need the text file to contain this code.

Thanks.
Comment
Watch Question

CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Remote Training and Programming
CERTIFIED EXPERT
Top Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Dustin StanleyEntrepreneur

Author

Commented:
Crystal how would I call or pass the parameters to that code:

????
Public Sub CallSaveStringAsTextFile()
SaveStringAsTextFile ("C:\Users\Station\Documents\Access XML Save Files\Test14.xml")
End Sub

Public Sub SaveStringAsTextFile(psPathFile As String, psFileContents)
'160730 strive4peace
   Dim iFile As Integer
   
   iFile = FreeFile
   Open psPathFile For Output As iFile
   Print #iFile, psFileContents
   Close iFile

End Sub

Open in new window



Lets say this is the code:

 "uahfiuaheuifgha;vghauivgh;iuehgfehfiaedghewgh" _
& "iuasfiuasfhasoieiedoiuesdsglueildsugilsudgileglisurlvgusli"

Where would I place it?


("C:\Users\Station\Documents\Access XML Save Files\Test14.xml") is where the file output will go. Thanks!
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and Programming
CERTIFIED EXPERT
Top Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
[ fanpages ]Consultant (Development Team Lead, Senior Support Engineer, and Technical Test Leader)
CERTIFIED EXPERT

Commented:
Another method (as a "one-liner" statement, just to demonstrate it can be done!)...

   Code = "uahfiuaheuifgha;vghauivgh;iuehgfehfiaedghewgh" & _
          "iuasfiuasfhasoieiedoiuesdsglueildsugilsudgileglisurlvgusli" & _
          "..."
       
   Call CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Users\Station\Documents\Access XML Save Files\Test14.xml", 2&, True).WriteLine(Code)

Open in new window

Dustin StanleyEntrepreneur

Author

Commented:
Just BRILLIANT! Thank you so much!
[ fanpages ]Consultant (Development Team Lead, Senior Support Engineer, and Technical Test Leader)
CERTIFIED EXPERT

Commented:
You're welcome.

Oh.
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and Programming
CERTIFIED EXPERT
Top Expert 2015

Commented:
you're welcome, Dustin ~ happy to help

(fanpages - interesting method -- but might that not leave a dangling object since CreateObject is used?)
[ fanpages ]Consultant (Development Team Lead, Senior Support Engineer, and Technical Test Leader)
CERTIFIED EXPERT

Commented:
"(fanpages - interesting method -- but might that not leave a dangling object since CreateObject is used?)"

Oh yes, I am very aware of that, but once an Exit Sub or End Sub is reached, that should release the resources.

This is not the method I would use; I just wanted to demonstrate it didn't need to be excessive.

I would choose, as I suspect you were suggesting, closing the objects (where necessary), & setting them to Nothing.

Interesting to see PatHartman receive recognition/points when that proposed solution missed the point of the question.
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and Programming
CERTIFIED EXPERT
Top Expert 2015

Commented:
thanks, fanpages. "once an Exit Sub or End Sub is reached, that should release the resources" -- not necessarily ...

Pat has helped Dustin a lot. While the solution turned out to be something else, it was hard to tell what he really wanted from his first post. I'm fine with splitting points. Had Dustin noticed your idea when he was splitting them, you'd probably have gotten an assist too.  You were both posting about the same time though.
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Getting points also surprised me since it turns out that the question wasn't related to Word.  Dustin, please don't feel you need to award points for participation.  It diminishes the value of the actual awards.  I come from an era where people actually earned their trophies so I am not offended to not get points if I didn't contribute to the answer.
[ fanpages ]Consultant (Development Team Lead, Senior Support Engineer, and Technical Test Leader)
CERTIFIED EXPERT

Commented:
thanks, fanpages. "once an Exit Sub or End Sub is reached, that should release the resources" -- not necessarily ...

That is why I put should in italics.
Dustin StanleyEntrepreneur

Author

Commented:
Crystal
Pat has helped Dustin a lot.
Yes this is VERY TRUE! Thank you!

Had Dustin noticed your idea when he was splitting them, you'd probably have gotten an assist too.  You were both posting about the same time though.
Exactly! I had closed it and then seen it right after.

Pat
I come from an era where people actually earned their trophies so I am not offended to not get points if I didn't contribute to the answer.
I totally understand and 99% of the time your solutions work awesome for me so this showed I had no doubt in your solution. Crystals was a dead on quick solution so I chose this as the main solution. But like I said I had no doubt in your answer so it assisted me. I also keep your comments in mind so I can refer back to them when the other solutions do not work for a new problem.

THANK YOU ALL FOR THE HELP! IT TRULY MEANS ALOT TO ME! THANKS!
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Thanks Dustin.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.