Uploading text file to form

L

LEU

Is there an easer way to upload a text file into a form than what I have
written below. Some of my forms are rather large.

Dim tempfile
Dim bk1, bk2, bk3, bk4
ChangeFileOpenDirectory "C:\History\"
With Dialogs(wdDialogFileOpen)
If .Display = -1 Then
Application.DefaultSaveFormat = "Text"
tempfile = .Name
Open tempfile For Input As #1
Input #1, bk1, bk2, bk3, bk4
Cmd1.Value = bk1
Cmd2.Value = bk2
Text1.Value = bk3
Text2.Value = bk4
Close #1
End If
End With
 
J

Jezebel

Hard to generalize: 'easier' can mean a lot of things. By the look of it,
ease of maintenance might be more important than running efficiency.

One approach would be to read your text file directly into an array --

Dim pValues() as string

pFileNum = FREEFILE (you shouldn't assume #1 will be valid)
Open tempfile For Input As #pFileNum
pValues = split(input(LOF(pFileNum),pFileNum), ",")
Close pFileNum

Then set up a map of positions in the Values() array to the corresponding
controls --

Set Map(0) = cmd1
Set Map(1) = cmd2
Set Map(2) = Text1
:

Then run a loop like --

for pIndex = lBound(pValues) to Ubound(pValues)
Map(pIndex) = pValues(pIndex)
Next
 
L

LEU

Thanks that worked very nice.

I use the following to open the directory I want to be in, but it does not
always work. Sometimes it opens just the “C†drive. What am I doing wrong?

ChangeFileOpenDirectory "C:\History\WCR\"
With Dialogs(wdDialogFileOpen)
If .Display = -1 Then
tempfile = .Name
‘and then the rest of my macro
 
L

LEU

I figured it out, I just needed to set my "DefaultFilePath"

Thanks again for all your help....
 

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