Problem with code

M

Mike

I have created a DB in Access 2000. There is a form with a custom
"BrowseButton" which opens an "open file" dialog box. It works fine in Access
2000, but it doesn't in Access 2003. Even the error handler reports nothing.
Here is the code for the "BrowseButton" on-click event and some additional
related code:

Option Compare Database
Option Explicit

Private Type OPENFILENAME
lStructSize As Long
hwnd 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

Private Declare Function GetOpenFileName _
Lib "comdlg32.dll" Alias "GetOpenFileNameA" _
(pOpenfilename As OPENFILENAME) As Long

Private Declare Function GetSaveFileName _
Lib "comdlg32.dll" Alias "GetSaveFileNameA" _
(pOpenfilename As OPENFILENAME) As Long

Private Const OFN_FILEMUSTEXIST = &H1000
Private Const OFN_HIDEREADONLY = &H4
Private Const OFN_OVERWRITEPROMPT = &H2
Private Const OFN_PATHMUSTEXIST = &H800
Private Const OFN_SAVE = 0
Private Const OFN_OPEN = 1

Private Function DialogFile(wMode As Integer, szDialogTitle As String,
szFileName As String, szFilter As String, szDefDir As String, szDefExt As
String) As String
Dim X As Long, OFN As OPENFILENAME, szFile As String, szFileTitle As String
With OFN
.lStructSize = Len(OFN)
.hwnd = Me.hwnd
.lpstrTitle = szDialogTitle
.lpstrFile = szFileName & String$(250 - Len(szFileName), 0)
.nMaxFile = 255
.lpstrFileTitle = String$(255, 0)
.nMaxFileTitle = 255
.lpstrFilter = NullSepString(szFilter)
.nFilterIndex = 2
.lpstrInitialDir = szDefDir
.lpstrDefExt = szDefExt
If wMode = 1 Then
OFN.Flags = OFN_HIDEREADONLY Or OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST
X = GetOpenFileName(OFN)
Else
OFN.Flags = OFN_HIDEREADONLY Or OFN_OVERWRITEPROMPT Or OFN_PATHMUSTEXIST
X = GetSaveFileName(OFN)
End If
If X <> 0 Then
If InStr(.lpstrFile, Chr$(0)) > 0 Then
szFile = Left$(.lpstrFile, InStr(.lpstrFile, Chr$(0)) - 1)
End If
DialogFile = szFile
Else
DialogFile = ""
End If
End With
End Function

Private Function NullSepString(ByVal CommaString As String) As String
Dim intInstr As Integer
Const vbBar = "|"
Do
intInstr = InStr(CommaString, vbBar)
If intInstr > 0 Then Mid$(CommaString, intInstr, 1) = vbNullChar
Loop While intInstr > 0
NullSepString = CommaString
End Function

Private Sub BrowseButton_Click()
On Error GoTo ErrHandler
With Me.FilePath
.Value = DialogFile(1, _
"Choose file...", _
vbNullString, _
"Microsoft Excel Book (*.xls)|*.xls", _
CurDir(), _
vbNullString)
End With
ExitHere:
Exit Sub
ErrHandler:
With Err
MsgBox "Error: " & .Number & vbCrLf & .Description, _
vbCritical Or vbOKOnly, .Source
End With
Resume ExitHere
End Sub

Why doesn't it work in Access 2003?
Thank you in advance for any tips!
 
K

Klatuu

Check your references. Since you are using 20003, the Excel and Office
libraries have changed since 2000
 
6

'69 Camaro

Hi, Mike.
It works fine in Access
2000, but it doesn't in Access 2003.

Your code works as expected in Access 2003. Perhaps your expectations
differ from mine. When the CurDir( ) is set as the default directory, the
path given in the "Default Database Folder" option will be used. If this
path is invalid (as it may very well be if you are now using this database on
a different computer), then the default directory will become the last
directory browsed for.

Perhaps you could explain in further detail what is happening and what you
expect to happen.

HTH.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)
Beware to those who use munged addresses: known newsgroup E-mail harvesters
for spammers are (e-mail address removed) and (e-mail address removed)

- - -
When you see correct answers to your question posted in Microsoft's Online
Community, please sign in to the Community and mark these posts as "Answers,"
so that all may benefit by filtering on "Answered questions" and quickly
finding the right answers to similar questions. Remember that questions
answered the quickest are often from those who have a history of rewarding
the contributors who have taken the time to answer questions correctly.
 
6

'69 Camaro

If this
path is invalid (as it may very well be if you are now using this database on
a different computer)

Strike that. This path isn't going to be invalid unless there's a problem
with the Windows Registry. Short of corruption, you'd have to intentionally
make it invalid, like cloning another computer and making changes to the
Windows Registry that are inconsistent with the original computer's
configuration. Very unlikely.

HTH.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)
 
M

Mike

The problem was in antivirus software. I changed some settings there and now
everything works fine.
 

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