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

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 07-02-2010, 11:02 AM   #1
davesmith202
Employee of Access World
 
Join Date: Jul 2001
Posts: 522
Thanks: 7
Thanked 0 Times in 0 Posts
davesmith202 is on a distinguished road
Simple text import

I have a txt file that I want to import into a string. What is the simplest way of doing this?

Thanks,

Dave

davesmith202 is offline   Reply With Quote
Old 07-02-2010, 12:00 PM   #2
vbaInet
AWF VIP
 
Join Date: Jan 2010
Location: U.K.
Posts: 26,374
Thanks: 0
Thanked 2,423 Times in 2,389 Posts
vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all
Re: Simple text import

Something like this should work:
Code:
public function GetMyFile(pathToFile as string) as string
    dim fileNum As Integer
    
    getmyfile = ""
    fileNum = freeFile

    open pathtofile For Input As #fileNum
        Input #fileNum, GetMyFile
    close #fileNum
end function

Last edited by vbaInet; 07-02-2010 at 01:18 PM.
vbaInet is offline   Reply With Quote
Old 07-02-2010, 01:02 PM   #3
davesmith202
Employee of Access World
 
Join Date: Jul 2001
Posts: 522
Thanks: 7
Thanked 0 Times in 0 Posts
davesmith202 is on a distinguished road
Re: Simple text import

Do I call it like this?
Code:
strdata = GetMyFile("C:\Folders\Data\testdata\B21.txt")

davesmith202 is offline   Reply With Quote
Old 07-02-2010, 01:18 PM   #4
vbaInet
AWF VIP
 
Join Date: Jan 2010
Location: U.K.
Posts: 26,374
Thanks: 0
Thanked 2,423 Times in 2,389 Posts
vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all
Re: Simple text import

I've amended the code so now you should be able to call it like that.
vbaInet is offline   Reply With Quote
Old 07-02-2010, 01:30 PM   #5
davesmith202
Employee of Access World
 
Join Date: Jul 2001
Posts: 522
Thanks: 7
Thanked 0 Times in 0 Posts
davesmith202 is on a distinguished road
Re: Simple text import

This may sound like a daft question but does that pick up just the first line in the text file?
davesmith202 is offline   Reply With Quote
Old 07-02-2010, 01:31 PM   #6
vbaInet
AWF VIP
 
Join Date: Jan 2010
Location: U.K.
Posts: 26,374
Thanks: 0
Thanked 2,423 Times in 2,389 Posts
vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all
Re: Simple text import

Always good to try it out first and see what the result is
vbaInet is offline   Reply With Quote
Old 07-02-2010, 01:32 PM   #7
davesmith202
Employee of Access World
 
Join Date: Jul 2001
Posts: 522
Thanks: 7
Thanked 0 Times in 0 Posts
davesmith202 is on a distinguished road
Re: Simple text import

I did. I got the first line only!

davesmith202 is offline   Reply With Quote
Old 07-02-2010, 01:39 PM   #8
DCrake
Remembered
 
DCrake's Avatar
 
Join Date: Jun 2005
Location: Burnley, Lancashire
Posts: 8,634
Thanks: 8
Thanked 325 Times in 208 Posts
DCrake is a glorious beacon of light DCrake is a glorious beacon of light DCrake is a glorious beacon of light DCrake is a glorious beacon of light DCrake is a glorious beacon of light DCrake is a glorious beacon of light
Send a message via Skype™ to DCrake
Re: Simple text import

Your original post did not indicate that there was more than on row of text. Wou actually wanting to do with the text once you have got it. also what does the contents look like? Can you supply a same txt file?
__________________
David Crake


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
The Home of Simple Software Solutions.

O/S Windows XP (SP3) & Windows 7 64bit
Access 2003 (version 11.0)
Access 2007 (version 12.0)

Remember when posting sample databases you will get a better response if it is pre Access 2007 - not all people have it installed.
DCrake is offline   Reply With Quote
Old 07-02-2010, 01:39 PM   #9
davesmith202
Employee of Access World
 
Join Date: Jul 2001
Posts: 522
Thanks: 7
Thanked 0 Times in 0 Posts
davesmith202 is on a distinguished road
Re: Simple text import

Not sure how to get the whole file!
davesmith202 is offline   Reply With Quote
Old 07-02-2010, 01:45 PM   #10
DCrake
Remembered
 
DCrake's Avatar
 
Join Date: Jun 2005
Location: Burnley, Lancashire
Posts: 8,634
Thanks: 8
Thanked 325 Times in 208 Posts
DCrake is a glorious beacon of light DCrake is a glorious beacon of light DCrake is a glorious beacon of light DCrake is a glorious beacon of light DCrake is a glorious beacon of light DCrake is a glorious beacon of light
Send a message via Skype™ to DCrake
Re: Simple text import

Like I said what does the text file contain?

Freetext, Delimited fields, fixed width fields, something else?

How many rows does it contains?
Can they vary?
Cany you post a sample file on the forum to look at?
__________________
David Crake


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
The Home of Simple Software Solutions.

O/S Windows XP (SP3) & Windows 7 64bit
Access 2003 (version 11.0)
Access 2007 (version 12.0)

Remember when posting sample databases you will get a better response if it is pre Access 2007 - not all people have it installed.
DCrake is offline   Reply With Quote
Old 07-02-2010, 01:48 PM   #11
davesmith202
Employee of Access World
 
Join Date: Jul 2001
Posts: 522
Thanks: 7
Thanked 0 Times in 0 Posts
davesmith202 is on a distinguished road
Re: Simple text import

Looks like our posts crossed there DCrake. Sample data in text file:

Quote:
[Event "?"]
[Site "Europe"]
[Date "1620.??.??"]
[Round "?"]
[White "Greco, Gioacchino"]
[Black "NN"]
[Result "*"]
[ECO "B21"]

1. e4 c5 2. f4 e6 3. Nf3 Nc6 4. c3 d5 5. e5 Be7 6. d4 c4 7. Be2 Bh4+ 8. g3
Be7 9. Be3 Bd7 10. Nbd2 Nh6 11. b3 b5 12. a4 a6 13. axb5 axb5 14. b4 O-O
15. O-O Nf5 16. Bf2 Rxa1 17. Qxa1 Nxb4 18. cxb4 Bxb4 19. Qb1 Qa5 20. Qc2 Bc3
21. h3 *

[Event "?"]
[Site "Europe"]
[Date "1620.??.??"]
[Round "?"]
[White "NN"]
[Black "Greco, Gioacchino"]
[Result "0-1"]
[ECO "B21"]

1. e4 c5 2. f4 Nc6 3. Nf3 d6 4. Bc4 Nh6 5. O-O Bg4 6. c3 e6 7. h3 Bxf3
8. Qxf3 Qd7 9. d3 O-O-O 10. f5 Ne5 11. Qe2 Nxc4 12. Bxh6 Na5 13. b4 Nc6
14. Bd2 exf5 15. exf5 f6 16. b5 Ne7 17. Qe6 Qxe6 18. fxe6 Ng6 19. d4 d5
20. Be3 c4 21. Bc1 Re8 22. Re1 Bd6 23. a4 Nf8 24. Nd2 Nxe6 25. Nf3 g5 26. Nh2
h5 27. a5 Rhg8 28. a6 b6 29. Nf1 f5 30. Ne3 Nc7 31. Rf1 f4 32. Nd1 Ne6
33. Ra2 g4 34. Nf2 f3 35. hxg4 hxg4 36. Nh1 0-1

[Event "?"]
[Site "London"]
[Date "1794.??.??"]
[Round "?"]
[White "Atwood, George"]
[Black "Philidor, Francois Andre Dani"]
[Result "1-0"]
[ECO "B21"]

1. e4 c5 2. f4 Nc6 3. Nf3 e6 4. c3 d5 5. e5 f5 6. d4 Nh6 7. h3 Qb6 8. b3 Bd7
9. Be3 Nf7 10. Qd2 O-O-O 11. Qf2 cxd4 12. Nxd4 Nxd4 13. Bxd4 Qc6 14. Nd2 b6
15. a4 Bc5 16. Bb5 Bxd4 17. Qxd4 Qc5 18. Nf3 Bxb5 19. Qxc5+ bxc5 20. axb5 Kb7
21. Ke2 Ra8 22. Ra6 Rhe8 23. Rd1 Nd8 24. Ne1 c4 25. bxc4 dxc4 26. Rd7+ Kc8
27. Rxg7 Rb8 28. Raxa7 Rxb5 29. Rac7+ Kb8 30. Rxc4 Rb7 31. Rb4 Rxb4 32. cxb4
Nc6 33. Nd3 Re7 34. Rxe7 Nxe7 35. Nc5 Ng6 36. Nxe6 Kc8 37. Ke3 Kd7 38. Nd4
Ne7 39. g4 Ke8 40. g5 1-0
I have a parsing routine already that manipulates the data. I just need to get it into a string!
davesmith202 is offline   Reply With Quote
Old 07-02-2010, 01:54 PM   #12
DCrake
Remembered
 
DCrake's Avatar
 
Join Date: Jun 2005
Location: Burnley, Lancashire
Posts: 8,634
Thanks: 8
Thanked 325 Times in 208 Posts
DCrake is a glorious beacon of light DCrake is a glorious beacon of light DCrake is a glorious beacon of light DCrake is a glorious beacon of light DCrake is a glorious beacon of light DCrake is a glorious beacon of light
Send a message via Skype™ to DCrake
Re: Simple text import

Based on the solution by vbaINet

Code:
public function GetMyFile(pathToFile as string) 
    dim fileNum As Integer
    Dim myString As String
    fileNum = freeFile

    open pathtofile For Input As #fileNum
       Do Until EOF(#fileNum)
          Input #fileNum, MyString
          Debug.Print MyString
       Loop

    close #fileNum
end function
__________________
David Crake


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
The Home of Simple Software Solutions.

O/S Windows XP (SP3) & Windows 7 64bit
Access 2003 (version 11.0)
Access 2007 (version 12.0)

Remember when posting sample databases you will get a better response if it is pre Access 2007 - not all people have it installed.
DCrake is offline   Reply With Quote
Old 07-02-2010, 01:57 PM   #13
davesmith202
Employee of Access World
 
Join Date: Jul 2001
Posts: 522
Thanks: 7
Thanked 0 Times in 0 Posts
davesmith202 is on a distinguished road
Re: Simple text import

I get a syntax error and compile error on this line:

Code:
Do Until EOF(#fileNum)
davesmith202 is offline   Reply With Quote
Old 07-02-2010, 02:02 PM   #14
vbaInet
AWF VIP
 
Join Date: Jan 2010
Location: U.K.
Posts: 26,374
Thanks: 0
Thanked 2,423 Times in 2,389 Posts
vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all
Re: Simple text import

That was all aircode, you don't need the hash (#) on that line.
vbaInet is offline   Reply With Quote
Old 07-02-2010, 02:05 PM   #15
DCrake
Remembered
 
DCrake's Avatar
 
Join Date: Jun 2005
Location: Burnley, Lancashire
Posts: 8,634
Thanks: 8
Thanked 325 Times in 208 Posts
DCrake is a glorious beacon of light DCrake is a glorious beacon of light DCrake is a glorious beacon of light DCrake is a glorious beacon of light DCrake is a glorious beacon of light DCrake is a glorious beacon of light
Send a message via Skype™ to DCrake
Re: Simple text import

Code:
     Do While Not EOF(iFileNum)
        Line Input #iFileNum, sBuf

__________________
David Crake


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
The Home of Simple Software Solutions.

O/S Windows XP (SP3) & Windows 7 64bit
Access 2003 (version 11.0)
Access 2007 (version 12.0)

Remember when posting sample databases you will get a better response if it is pre Access 2007 - not all people have it installed.
DCrake is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Table update on text input string solbane Modules & VBA 1 06-01-2010 11:15 PM
import linux text files into access database selahlynch Modules & VBA 13 02-25-2010 01:11 AM
Table structure / planning - help please KellyGroom Tables 4 09-13-2007 03:28 AM
check if text files exist before import to tables alienscript Modules & VBA 1 09-04-2006 11:34 PM
import text file catt Modules & VBA 16 04-21-2005 05:14 AM




All times are GMT -8. The time now is 07:59 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