1

I have created a basic form in access that has a couple of buttons and text fields. I want the button to do something along the lines of when I click it, it should bring up a browse file dialog and when I select a file, change the text field to the path of the file.

My question is how can I program this? Specifically, do I create a module that goes like?

sub command1_onClick()
  ..bla bla...
end sub

Or are forms programmed differently? How can I tie a function to a button?

2 Answers 2

2

You can add actions to the button. In the properties window of the button is an Event-Tab. Click on On Click and select Event Procedure. You are then taken to the VBA Editor (ALT+F11) into following procedure:

Private Sub Command1_Click()

End Sub

In that procedure, you can use an API call to open the standard file dialog:

API: Call the standard Windows File Open/Save dialog box

The TestIt function in above link shows you hove to open the dialog and get the path. You can set your textbox to that path like this:

Me!Text1 = strPath

strPath has to be populated with the path you get from the file dialog.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, Application.FileDialog looks way easier ;) @harper89
1

You will want to look at the file dialog property.

Dim fDialog As Office.FileDialog

MSDN File Dialog

The above shows an example that is performed on a button click

The example adds the selected file to a listbox, for your situation you would want to use a simple textbox instead of the listbox.

You may want to set AllowMultiSelect to false to ignore the looping part if you only want one item. (this would simplify the code in the example for you.

.AllowMultiSelect = false

I may be a lil rusty but then you would want to do something like this(someone edit or correct me if I am way off)

Assuming you used varFile

(Dim varFile As Variant)

Me.TextBox1.Text = varFile

EDIT: After encountering error

It seems the error can come froma few things. Check to make sure the reference is there. Also you may need to add the Microsoft DAO 3.6 Object Library reference.

See the "More information" section of this Link (Hopefully 2002 is close enough to 2003) . It may have a few more helpful tips if that doesn't solve the problem.

7 Comments

Dim fDialog As Office.FileDialog << I'm getting an error at this line saying "User-defined type not define". Do you know why I'm getting this?
@ shubham The link says "Requires reference to Microsoft Office 11.0 Object Library." Do you have that? (not sure if thats the cause)
Yes I am, I went to tools>references and made sure 11.0 Object Library was selected. I'm using Access 2003 BTW
could it have something to do with dao or adodb?
@Shubham if its imported then hmmm. I am not sure, I will comment if I think of anything.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.