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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strange common dialog error 1

Status
Not open for further replies.

AppStaff

Programmer
Sep 21, 2002
146
US
PROBLEM: Strange Bar Characters being returned in file name and path being returned from common dialog ("|")

This seems to work fine up until the final step. The strange thing is that the message box pops up showing the correct file name and path but it errors out when attempting to write to the field because it says there are too many characters. When I check the variable results in the immediate window it shows cdlg.name as "my path and file name" and "|||||||||||||||||||||||||||||||||" bars like this on the end. Anyone have a solution or explanation about this?

Here is my code:

Private Sub cmdInstall_Click()
Dim cdlg As New CommonDialogAPI

Dim lngFormName As Long
Dim lngAppInstance As Long
Dim strInitDir As String
Dim strFileFilter As String
Dim lngResult As Long

lngFormName = Me.hwnd
lngAppInstance = Application.hWndAccessApp
strInitDir = "C:\My Documents\"
strFileFilter = "Access Databases (*.mdb, *.mde)" & Chr(0) & "*.mdb; *.mde" & Chr(0)

lngResult = cdlg.OpenFileDialog(lngFormName, lngAppInstance, strInitDir, strFileFilter)

If cdlg.GetStatus = True Then
MsgBox "You selected file: " & cdlg.GetName
Me.InstallFile = Trim(cdlg.GetName)
Else
MsgBox "No file selected."
End If
End Sub

 
The filename is returned as a null terminated string that is 256 characters long. In order for you to use it, you need to remove the trailing null characters (appearing as "|")
Code:
Public Function TrimNulls(strInput as String) as String
Dim x As Integer
    
x = InStr(strInput, vbNullChar)
If x > 0 Then
  TrimNulls = Left(strInput, x - 1)
Else
  TrimNulls = strInput
End If

End Function

Then instead of:

Code:
Me.InstallFile = Trim(cdlg.GetName)

use

Code:
Me.InstallFile = TrimNulls(cdlg.GetName)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top