Filepath in code via form

  • Thread starter Thread starter ant1983
  • Start date Start date
A

ant1983

Hi,

I have some code (not written by me) that refers to a "file path" i.e.
"G:\Garfield\Pictures"... I guess that path is hard coded but i dont want
this and want to replace it with some code that can be changed via a form.

What i want is a form with a text box and a button to open the "Open Folder"
window so that the user can scroll to the folder and click ok and then the
path should display in the text box and should be inserted into the code so
that the other forms can use that new file path. Can this be done??

I guess there would also be a cancel and save button on the form so that the
newly added path can be saved in the code...

Where do i put what code? LOL

Thanks...

Thanks
 
Take a look at http://www.mvps.org/access/api/api0002.htm at "The Access
Web". You'd copy everything in the shaded area (between Code Starts and Code
Ends) into a new module (not a class module nor a module associated with a
form or report), and then put the following code inside the Click event of
your button:

Dim strFolderName As String

strFolderName = BrowseFolder("What Folder you want to select?")
 
Thats not working!! (Did i mention i'm a complete novice??) (LOL)

Er... OK well this is the code that i have in a module currently:

====================================================
Option Compare Database
Option Explicit

Global Const gsFilesPath = "C:\Users\Wayne\Desktop\Door\Scans\"
'Global Const gsFilesPath = CurrentProject.Path & "\"
'Global Const gsFilesPath = "\\server\shared\"

Public Function GetPicture(autDocumentID As String) As String
Dim fso
Dim File As String

On Error GoTo myERR
Dim filePath As String

filePath = gsFilesPath & Trim(autDocumentID) & ".bmp"
Set fso = CreateObject("Scripting.FileSystemObject")

If Not fso.FileExists(filePath) Then
GetPicture = ""
Else
GetPicture = filePath
End If
Exit Function

myERR:
Select Case Err.Number
Case 2220
GetPicture = ""
Case Else
End Select

End Function

====================================================

Then i have a form called frmSearchCompany with an image on the form called
"img" and there is a table on that form to with a unique number (i.e.
numAccountNumber). If the user places his cursor on the unique number and
then clicks in the image the image chages to that of the same image name in
the above file path.

So what i want is for the above filepath not to be hard coded but for it to
be able to be changed by the user by doing that browse thing on another form.

The other form by the way is called frmMainMenu and has:

txtFileName;
cmdBrowse
cmdSave

So what i want is for the browse button to open that window (which it now
does thatnks to your code) but once i hit save it should kinda replace the
above filepath with that of the new filepath...

LOL

Now im even more confused!! :)

Please help!! Ive got no more hair left...
 
Public Function GetPicture(autDocumentID As String) As String
On Error GoTo myERR

Dim strFolder as String
Dim filePath As String

strFolder = BrowseFolder("What Folder you want to select?")
If Len(strFolder) > 0 Then
filePath = strFolder & Trim(autDocumentID) & ".bmp"

If Len(Dir(filePath)) = 0 Then
GetPicture = ""
Else
GetPicture = filePath
End If

End If

myExit:
Exit Function

myERR:
Select Case Err.Number
Case 2220
GetPicture = ""
Case Else
End Select
Resume myExit

End Function

This assumes that you copied all of the code from
http://www.mvps.org/access/api/api0002.htm at "The Access Web" into a new
modulue (not a class module nor a module associated with a form or report)
into your application.

Note that I removed your use of FSO. There's seldom any reason to use FSO:
almost all the functionality you can get from FSO can be obtained through
VBA functions.
 
Er...

Where does that code go???

Sorry :)

Douglas J. Steele said:
Public Function GetPicture(autDocumentID As String) As String
On Error GoTo myERR

Dim strFolder as String
Dim filePath As String

strFolder = BrowseFolder("What Folder you want to select?")
If Len(strFolder) > 0 Then
filePath = strFolder & Trim(autDocumentID) & ".bmp"

If Len(Dir(filePath)) = 0 Then
GetPicture = ""
Else
GetPicture = filePath
End If

End If

myExit:
Exit Function

myERR:
Select Case Err.Number
Case 2220
GetPicture = ""
Case Else
End Select
Resume myExit

End Function

This assumes that you copied all of the code from
http://www.mvps.org/access/api/api0002.htm at "The Access Web" into a new
modulue (not a class module nor a module associated with a form or report)
into your application.

Note that I removed your use of FSO. There's seldom any reason to use FSO:
almost all the functionality you can get from FSO can be obtained through
VBA functions.
 
The code I gave you replaces your code for the function with the same name.
 
Thanks Douglas but that doesnt really have the effect i was hoping for.

I;ve overcomplicated thsi way too much so gona simplify it:

My code in a module (modGeneral) is:

====================================================
Option Compare Database
Option Explicit

Global Const gsFilesPath = "C:\Users\Wayne\Desktop\Door\Scans\"
'Global Const gsFilesPath = CurrentProject.Path & "\"
'Global Const gsFilesPath = "\\server\shared\"

Public Function GetPicture(autDocumentID As String) As String
Dim fso
Dim File As String

On Error GoTo myERR
Dim filePath As String

filePath = gsFilesPath & Trim(autDocumentID) & ".bmp"
Set fso = CreateObject("Scripting.FileSystemObject")

If Not fso.FileExists(filePath) Then
GetPicture = ""
Else
GetPicture = filePath
End If
Exit Function

myERR:
Select Case Err.Number
Case 2220
GetPicture = ""
Case Else
End Select

End Function

====================================================

So referring to the very first line:

Global Const gsFilesPath = "C:\Users\Wayne\Desktop\Door\Scans\"

I want the "file path" in this line to be changeable by the user by going to
a form and writing the filepath in a text box. As i said before it just
refers to a folder with a whole bunch of pictures in it.

So, i was thinking of adding like a browse button to that form so that the
user doesnt need to type out the whole file path but that seems to be a
mission so my question now is how can we change that line so that it just
refers to a text box where the user can type the filepath?

Cheers man!!
 
If Len(Forms![NameOfForm]![NameOfTextbox]) > 0 Then
filePath = Forms![NameOfForm]![NameOfTextbox] & _
Trim(autDocumentID) & ".bmp"

If Len(Dir(filePath)) = 0 Then
GetPicture = ""
Else
GetPicture = filePath
End If

End If
 
Back
Top