Simple Form: How to copy a value from one object to another?

D

Dirk Diggler

hello everyone,

There is a simple form in Excel: one button, and one textbox. The button
has a CommandButton1_Click() procedure defined as a call to the
following subroutine:

Public Sub ChooseUniverse()
Dim sFile As String

sFile = Application.GetOpenFilename(FileFilter:= _
"TXT Files (*.txt), *.txt", FilterIndex:=1, _
Title:="Choose TXT File...")
End Sub

I tried to figure out if it was possible to pass the sFile value to the
textbox so that after choosing a file its path would be visible in the
textbox, but I failed at looking for some understandable examples.

Does anybody know it is possible and how can be done? I would appreciate
any suggestions or hints, or a link to some tutorial (with Excel).

thank you,
DD

ps. Follow-Up To: microsoft.public.excel.programming
 
B

Bob Flanagan

Dirk, you can do it like the following;

Private Sub CommandButton1_Click()
Dim sFile As String

sFile = Application.GetOpenFilename(FileFilter:= _
"TXT Files (*.txt), *.txt", FilterIndex:=1, _
Title:="Choose TXT File...")
If sFile <> "False" Then
Me.TextBox1.Text = sFile
End If
End Sub

Bob Flanagan
Macro Systems
http://www.add-ins.com
Productivity add-ins and downloadable books on VB macros for Excel
 
D

Dave Peterson

There are followups in .programming.

Dirk said:
hello everyone,

There is a simple form in Excel: one button, and one textbox. The button
has a CommandButton1_Click() procedure defined as a call to the
following subroutine:

Public Sub ChooseUniverse()
Dim sFile As String

sFile = Application.GetOpenFilename(FileFilter:= _
"TXT Files (*.txt), *.txt", FilterIndex:=1, _
Title:="Choose TXT File...")
End Sub

I tried to figure out if it was possible to pass the sFile value to the
textbox so that after choosing a file its path would be visible in the
textbox, but I failed at looking for some understandable examples.

Does anybody know it is possible and how can be done? I would appreciate
any suggestions or hints, or a link to some tutorial (with Excel).

thank you,
DD

ps. Follow-Up To: microsoft.public.excel.programming
 
D

Dirk Diggler

thank you, guys! I tried to use Value property of TextBox1, and
mistakenly referenced the function.

thanks,
DD
 
J

jan

Dirk,

This could do the job for you.
The subroutine ChooseUniverse I changed in a Function.

Private Sub CommandButton1_Click()
Dim sFile As Variant
sFile = ChooseUniverse
If sFile <> False Then
Me.TextBox1 = sFile
Else
Me.TextBox1 = Null
End If
End Sub

Public Function ChooseUniverse() As Variant
Dim sFile As String
sFile = Application.GetOpenFilename(FileFilter:= _
"TXT Files (*.txt), *.txt", FilterIndex:=1, _
Title:="Choose TXT File...")
ChooseUniverse = sFile
End Function

Jan
 

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