Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a BROWSE button in FORM

Status
Not open for further replies.

ciel220

Programmer
Jan 12, 2001
35
CA
Hi,

I'm creating a form for 'importing delimited text files'.
My first question is, how can I create a browse button for browing & selecting files? I don't want to use 'get external source --> import'.

Another question, I create a macro using TEXT TRANSFER. The delimited test file imported by this macro is in a mess. (That is, the delimited field stick together)
I then use 'get external data -> import' (instead of macros -> text transfer) to try, the import text file is ok. I am using the same delimited text file but only malfunction in TEXT TRANSFER. Anyone know what's wrong with it?

THANKS A BUNCH!

mm
 
To create a browse button,
1. Insert the ActiveX control "Microsoft Common Dialog Control" and then set the appropriate properties.
2. Create a command button with the word "Browse"
3. The OnClick event of the Browse button, should look like this:
Code:
Private Sub cmdBrowse_Click()

    dlgBrowse.ShowOpen   'dlgBrowse is the name I gave to the ActiveX Control
            
    MsgBox dlgBrowse.FileName
    
End Sub
 
Hi FancyPairie,

Thanks a lot!
However, if I don't have OLE server/license, i.e. I couldn't insert proper Active X control, is there any other way I could add a BROWSE button?

And do u know why when my import is messed up when I import using Macros(TEXT transfer)?

Thank you so much!
ciel
 
I believe the common dialog control comes with Office. The file you need is comdlg32.ocx. Search your hard drive for this file. If found, then register it via the Run command: Regsvr32 "c:\yourpath\comdlg32.ocx". If you don't have it, you should be able to download from Microsoft or other places. I just searched for "comdlg32.ocx" via Google and found several hits.

Not sure about your transfer problem. I try to stay away from importing files via the Transfer method (I generally do it via code). After the files have been imported or linked, things seem to slow up and act weird (i.e. cursor, etc.). Microsoft states the reason why it does this (can't remember why, read it about a year ago).
 
I've built db's for companies that also did not have the license to use the ocx controls. Here's what I used to open image files:

'Module declarations
Private cdlPath As String
Private cdlFileType As String

Public Const OFN_READONLY = &H1
Public Const OFN_OVERWRITEPROMPT = &H2
Public Const OFN_HIDEREADONLY = &H4
Public Const OFN_NOCHANGEDIR = &H8
Public Const OFN_SHOWHELP = &H10
Public Const OFN_ENABLEHOOK = &H20
Public Const OFN_ENABLETEMPLATE = &H40
Public Const OFN_ENABLETEMPLATEHANDLE = &H80
Public Const OFN_NOVALIDATE = &H100
Public Const OFN_ALLOWMULTISELECT = &H200
Public Const OFN_EXTENSIONDIFFERENT = &H400
Public Const OFN_PATHMUSTEXIST = &H800
Public Const OFN_FILEMUSTEXIST = &H1000
Public Const OFN_CREATEPROMPT = &H2000
Public Const OFN_SHAREAWARE = &H4000
Public Const OFN_NOREADONLYRETURN = &H8000
Public Const OFN_NOTESTFILECREATE = &H10000
Public Const OFN_NONETWORKBUTTON = &H20000
Public Const OFN_NOLONGNAMES = &H40000
Public Const OFN_EXPLORER = &H80000
Public Const OFN_NODEREFERENCELINKS = &H100000
Public Const OFN_LONGNAMES = &H200000
Public Const OFN_SHAREFALLTHROUGH = 2

Public Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

' API declarations
Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

Public Declare Function GetWindowWord Lib "user32" _
(ByVal HWnd As Long, ByVal lngIndex As Long) As Long

'Methods
Public Function GetPicturePath(ByVal HWnd As Long, ByRef strPath As String) As Boolean
On Error GoTo PicError
Dim ofn As OPENFILENAME, rtnVal As Long
Dim slashPos As Integer, slashTemp As Integer
Dim strLocalPath As String, strNewFile As String
Dim strTempPath As String

ofn.lStructSize = Len(ofn)
ofn.hwndOwner = HWnd
ofn.hInstance = GetWindowWord(HWnd, GWW_HINSTANCE)
ofn.lpstrFilter = _
"All Bitmaps (*.bmp)" + Chr$(0) + "*.bmp" + Chr$(0) + _
"All Gifs (*.gif)" + Chr$(0) + "*.gif" + Chr$(0) + _
"All JPEGs (*.jpg)" + Chr$(0) + "*.jpg" + Chr$(0) + _
"All Metafiles (*.wmf)" + Chr$(0) + "*.wmf" + Chr$(0)

Select Case cdlFileType
Case "bmp", "BMP"
ofn.nFilterIndex = 1
Case "gif", "GIF"
ofn.nFilterIndex = 2
Case "jpg", "JPG"
ofn.nFilterIndex = 3
Case "wmf", "WMF"
ofn.nFilterIndex = 4
Case Else
' default
End Select

ofn.lpstrFile = Space$(254)
ofn.nMaxFile = 255
ofn.lpstrFileTitle = Space$(254)
ofn.nMaxFileTitle = 255
If cdlPath = "" Then
ofn.lpstrInitialDir = GetDBPath()
Else
ofn.lpstrInitialDir = cdlPath
End If
ofn.lpstrTitle = "Select Background Picture"
ofn.flags = OFN_HIDEREADONLY

rtnVal = GetOpenFileName(ofn)
If (rtnVal) Then
strPath = Trim(ofn.lpstrFile)
strPath = Mid(strPath, 1, Len(strPath) - 1) ' Trim NULL
slashTemp = InStr(1, strPath, "\")
While slashTemp > 0
slashTemp = InStr(slashPos + 1, strPath, "\")
If slashTemp > 0 Then slashPos = slashTemp
Wend
If Len(strPath) > 0 Then
cdlPath = Mid(strPath, 1, slashPos - 1)
cdlFileType = Right(strPath, 3)
End If
Select Case Left(strPath, 1)
Case "B", "F" To "Z"
' If network path, convert to UNC
strTempPath = Mid(strPath, 4)
strPath = GetNetPath(Left(strPath, 1))
strPath = strPath & strTempPath
Case "A", "C"
' If not network path, copy file to
' network drive and return UNC path
strLocalPath = strPath
strPath = GetNetPath(Left(CurrentDb.Name, 1))
strPath = strPath & Mid(GetDBPath(), 4)
strNewFile = GetFileName(strLocalPath)
strPath = strPath & strNewFile
Call FileCopy(strLocalPath, strPath)
Case "\"
' UNC path OK
Case Else
Call LogError("modUtility", "GetPicturePath", "None", _
"Drive problem: " & strPath)
End Select
GetPicturePath = True
Else
' Cancel was pressed
End If
PicExit:
Exit Function
PicError:
Call LogError("modUtility", "GetPicturePath", Err.Number, Err.Description)
Resume PicExit
End Function

This should point you in the right direction. You need to pass the Hwnd of the active form to this procedure to make sure the Common Dialog knows who its parent is, otherwise it will position itself at the top left corner of the screen.


VBSlammer
redinvader3walking.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top