I'm looking to find out how necessary the function is that replaces a given character in a string with another character (ReplaceG).
I'm interested in compacting this code as much as possible without losing its functionality.
As you can tell, this is part of the code for importing a spreadsheet into Access by browsing.
If you want to see the rest of the code, let me know.
Public Function ReplaceG(sString As String, sFind As String, sReplace As String) As String
On Error GoTo Proc_Err
Dim lPtr As Long
Dim sTmp, sRetVal As String
sTmp = sString
If sFind <> sReplace Then
lPtr = InStr(1, sTmp, sFind)
Do Until lPtr = 0
sRetVal = sRetVal & Left(sTmp, lPtr - 1) & sReplace
sTmp = Right(sTmp, Len(sTmp) - (lPtr + Len(sFind) - 1))
lPtr = InStr(1, sTmp, sFind)
Loop
End If
ReplaceG = sRetVal & sTmp
Proc_Exit:
Exit Function
Proc_Err:
MsgBox Err.Description
Resume Proc_Exit
End Function
Public Function GetOpenFileNameGs(objForm As Form) As String
Dim sInitialDir, sFileFilter As String
Dim lReturn, lFlag As Long
Dim OpenFile As OPENFILENAME
sInitialDir = "C:\"
sFileFilter = "Excel Files (*.XLS)|*.XLS|Comma Delimited Files (*.CSV)|*.CSV|" & _
"DEL File (*.DEL)|*.DEL|All Files|*.*||"
lFlag = 0
OpenFile.lStructSize = Len(OpenFile)
OpenFile.hWndOwner = objForm.Hwnd
OpenFile.hInstance = Application.hWndAccessApp
OpenFile.lpstrFilter = ReplaceG(sFileFilter, "|", vbNullChar)
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = String(257, 0)
OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
OpenFile.lpstrFileTitle = OpenFile.lpstrFile
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = sInitialDir
OpenFile.lpstrTitle = "Open File"
OpenFile.flags = lFlag
lReturn = GetOpenFileName(OpenFile)
If lReturn = 0 Then
GetOpenFileNameGs = ""
Else
GetOpenFileNameGs = ReplaceG(OpenFile.lpstrFile & "", vbNullChar, "")
End If
End Function
I'm interested in compacting this code as much as possible without losing its functionality.
As you can tell, this is part of the code for importing a spreadsheet into Access by browsing.
If you want to see the rest of the code, let me know.
Public Function ReplaceG(sString As String, sFind As String, sReplace As String) As String
On Error GoTo Proc_Err
Dim lPtr As Long
Dim sTmp, sRetVal As String
sTmp = sString
If sFind <> sReplace Then
lPtr = InStr(1, sTmp, sFind)
Do Until lPtr = 0
sRetVal = sRetVal & Left(sTmp, lPtr - 1) & sReplace
sTmp = Right(sTmp, Len(sTmp) - (lPtr + Len(sFind) - 1))
lPtr = InStr(1, sTmp, sFind)
Loop
End If
ReplaceG = sRetVal & sTmp
Proc_Exit:
Exit Function
Proc_Err:
MsgBox Err.Description
Resume Proc_Exit
End Function
Public Function GetOpenFileNameGs(objForm As Form) As String
Dim sInitialDir, sFileFilter As String
Dim lReturn, lFlag As Long
Dim OpenFile As OPENFILENAME
sInitialDir = "C:\"
sFileFilter = "Excel Files (*.XLS)|*.XLS|Comma Delimited Files (*.CSV)|*.CSV|" & _
"DEL File (*.DEL)|*.DEL|All Files|*.*||"
lFlag = 0
OpenFile.lStructSize = Len(OpenFile)
OpenFile.hWndOwner = objForm.Hwnd
OpenFile.hInstance = Application.hWndAccessApp
OpenFile.lpstrFilter = ReplaceG(sFileFilter, "|", vbNullChar)
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = String(257, 0)
OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
OpenFile.lpstrFileTitle = OpenFile.lpstrFile
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = sInitialDir
OpenFile.lpstrTitle = "Open File"
OpenFile.flags = lFlag
lReturn = GetOpenFileName(OpenFile)
If lReturn = 0 Then
GetOpenFileNameGs = ""
Else
GetOpenFileNameGs = ReplaceG(OpenFile.lpstrFile & "", vbNullChar, "")
End If
End Function