By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,482 Members | 1,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,482 IT Pros & Developers. It's quick & easy.

InStr and StrReverse

imrosie
100+
P: 222
Hello (from a newbie),


I have an image database that pulls in a copy of a file (stores the link in database). The image shows up in my subform. I then show the name of file in a control called image description, however, it shows the entire link:
D:\imageDogs\ChestertheDog.jpg

I'm looking for good example code for StrRevers (InStr)J, so I can somehow reverse the string, cut off the 'ChestertheDog.jpg and display only the ChestertheDog in the control. I also have another control box which I'd like the '.jpg' portion to go to.

Has anyone done something like this? Help...I've been playing around with samples from the Web and nothings working. thanks

Rosie
Jun 7 '07 #1
Share this Question
Share on Google+
20 Replies

FishVal
Expert 2.5K+
P: 2,653
Hello (from a newbie),


I have an image database that pulls in a copy of a file (stores the link in database). The image shows up in my subform. I then show the name of file in a control called image description, however, it shows the entire link:
D:\imageDogs\ChestertheDog.jpg

I'm looking for good example code for StrRevers (InStr)J, so I can somehow reverse the string, cut off the 'ChestertheDog.jpg and display only the ChestertheDog in the control. I also have another control box which I'd like the '.jpg' portion to go to.

Has anyone done something like this? Help...I've been playing around with samples from the Web and nothings working. thanks

Rosie
Hi!

Having dealed with a similar issue on batch file renaming some time ago I have written Class "FileFullPath". Hereby I attach the code.

Class module "FileFullPath":

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim strFilePath As String, strFileName As String, strFileExt As String
  3.  
  4. Public Property Get Path() As String
  5.     Path = strFilePath
  6. End Property
  7.  
  8. Public Property Let Path(ByVal strNewValue As String)
  9.     strFilePath = strNewValue
  10. End Property
  11.  
  12. Public Property Get FileName() As String
  13.     FileName = strFileName
  14. End Property
  15.  
  16. Public Property Let FileName(ByVal strNewValue As String)
  17.     strFileName = strNewValue
  18. End Property
  19.  
  20. Public Property Get Extension() As String
  21.     Extension = strFileExt
  22. End Property
  23.  
  24. Public Property Let Extension(ByVal strNewValue As String)
  25.     strFileExt = strNewValue
  26. End Property
  27.  
  28. Public Property Get FullName() As String
  29.  
  30.     With Me
  31.         FullName = .Path & "\" & .FileName & "." & .Extension
  32.     End With
  33.  
  34. End Property
  35.  
  36. Public Property Let FullName(ByVal strNewValue As String)
  37.  
  38.     With Me
  39.         .Path = CutSubString(strNewValue, "\")
  40.         .FileName = CutSubString(strNewValue, ".")
  41.         .Extension = strNewValue
  42.     End With
  43.  
  44. End Property
  45.  
  46. Private Function CutSubString(ByRef strInput As String, _
  47.                               ByVal strTerminator As String) As String
  48.  
  49.     Dim intNewPos As Integer, intPos As Integer
  50.  
  51.     intNewPos = 0
  52.     Do
  53.         intPos = intNewPos
  54.         intNewPos = InStr(intPos + 1, strInput, strTerminator, vbTextCompare)
  55.     Loop Until intNewPos = 0
  56.     CutSubString = Left(strInput, intPos - 1)
  57.     strInput = Right(strInput, Len(strInput) - intPos)
  58.  
  59. End Function
  60.  
  61.  
Form module:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub FileLink_AfterUpdate()
  3.  
  4.     Dim objFileLink As New FileFullPath
  5.  
  6.     With objFileLink
  7.         .FullName = Me![FileLink]
  8.         Me![Path] = .Path
  9.         Me![Name] = .FileName
  10.         Me![Extension] = .Extension
  11.     End With
  12.  
  13.     Set objFileLink = Nothing
  14.  
  15. End Sub
  16.  
  17.  
Jun 7 '07 #2

imrosie
100+
P: 222
Hi!

Having dealed with a similar issue on batch file renaming some time ago I have written Class "FileFullPath". Hereby I attach the code.

Class module "FileFullPath":

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim strFilePath As String, strFileName As String, strFileExt As String
  3.  
  4. Public Property Get Path() As String
  5.     Path = strFilePath
  6. End Property
  7.  
  8. Public Property Let Path(ByVal strNewValue As String)
  9.     strFilePath = strNewValue
  10. End Property
  11.  
  12. Public Property Get FileName() As String
  13.     FileName = strFileName
  14. End Property
  15.  
  16. Public Property Let FileName(ByVal strNewValue As String)
  17.     strFileName = strNewValue
  18. End Property
  19.  
  20. Public Property Get Extension() As String
  21.     Extension = strFileExt
  22. End Property
  23.  
  24. Public Property Let Extension(ByVal strNewValue As String)
  25.     strFileExt = strNewValue
  26. End Property
  27.  
  28. Public Property Get FullName() As String
  29.  
  30.     With Me
  31.         FullName = .Path & "\" & .FileName & "." & .Extension
  32.     End With
  33.  
  34. End Property
  35.  
  36. Public Property Let FullName(ByVal strNewValue As String)
  37.  
  38.     With Me
  39.         .Path = CutSubString(strNewValue, "\")
  40.         .FileName = CutSubString(strNewValue, ".")
  41.         .Extension = strNewValue
  42.     End With
  43.  
  44. End Property
  45.  
  46. Private Function CutSubString(ByRef strInput As String, _
  47.                               ByVal strTerminator As String) As String
  48.  
  49.     Dim intNewPos As Integer, intPos As Integer
  50.  
  51.     intNewPos = 0
  52.     Do
  53.         intPos = intNewPos
  54.         intNewPos = InStr(intPos + 1, strInput, strTerminator, vbTextCompare)
  55.     Loop Until intNewPos = 0
  56.     CutSubString = Left(strInput, intPos - 1)
  57.     strInput = Right(strInput, Len(strInput) - intPos)
  58.  
  59. End Function
  60.  
  61.  
Form module:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub FileLink_AfterUpdate()
  3.  
  4.     Dim objFileLink As New FileFullPath
  5.  
  6.     With objFileLink
  7.         .FullName = Me![FileLink]
  8.         Me![Path] = .Path
  9.         Me![Name] = .FileName
  10.         Me![Extension] = .Extension
  11.     End With
  12.  
  13.     Set objFileLink = Nothing
  14.  
  15. End Sub
  16.  
  17.  

All I can say is WOW! FishVal.....Iread thru it and I never expected to find something that seems so complete....I'm off to give this a try...thanks so much. I'll be back with the results.

Rosie
Jun 7 '07 #3

ADezii
Expert 5K+
P: 8,750
Hello (from a newbie),


I have an image database that pulls in a copy of a file (stores the link in database). The image shows up in my subform. I then show the name of file in a control called image description, however, it shows the entire link:
D:\imageDogs\ChestertheDog.jpg

I'm looking for good example code for StrRevers (InStr)J, so I can somehow reverse the string, cut off the 'ChestertheDog.jpg and display only the ChestertheDog in the control. I also have another control box which I'd like the '.jpg' portion to go to.

Has anyone done something like this? Help...I've been playing around with samples from the Web and nothings working. thanks

Rosie
To simply parse the File Path, and return only the Base File Name without the Extension, only requires a couple of lines of code. Maybe you can incorporate this into your code, if you so desire:
Expand|Select|Wrap|Line Numbers
  1. Dim strFilePath As String
  2.  
  3. strFilePath = "D:\imageDogs\ChestertheDog.jpg"        'Absolute File Path
  4.  
  5. 'File Name with Extension (.jpg)
  6. strFilePath = Right$(strFilePath, Len(strFilePath) - InStrRev(strFilePath, "\"))strFilePath = Left$(strFilePath, Len(strFilePath) - 4)    ' Base File Name
  7.    Debug.Print strFilePath     'see OUTPUT below
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. ChestertheDog
Jun 8 '07 #4

Expert 100+
P: 636
Hi

Hello (from a newbie),


I have an image database that pulls in a copy of a file (stores the link in database). The image shows up in my subform. I then show the name of file in a control called image description, however, it shows the entire link:
D:\imageDogs\ChestertheDog.jpg

I'm looking for good example code for StrRevers (InStr)J, so I can somehow reverse the string, cut off the 'ChestertheDog.jpg and display only the ChestertheDog in the control. I also have another control box which I'd like the '.jpg' portion to go to.

Has anyone done something like this? Help...I've been playing around with samples from the Web and nothings working. thanks

Rosie
Does this help ?

Expand|Select|Wrap|Line Numbers
  1. Sub test()
  2.     Dim Temp As String
  3.     Dim FName As String
  4.     Dim Ext As String
  5.     Temp = "D:\imageDogs\ChestertheDog.jpg"
  6.  
  7.     Ext = Mid(Temp, InStrRev(Temp, ".") + 1)
  8.  
  9.     FName = Left(Temp, InStrRev(Temp, ".") - 1)
  10.     FName = Mid(FName, InStrRev(Temp, "\") + 1)
  11.  
  12.     MsgBox "File Name = " & FName
  13.     MsgBox "Extention = " & Ext
  14.  
  15. End Sub
  16.  
MTB
Jun 8 '07 #5

imrosie
100+
P: 222
To simply parse the File Path, and return only the Base File Name without the Extension, only requires a couple of lines of code. Maybe you can incorporate this into your code, if you so desire:
Expand|Select|Wrap|Line Numbers
  1. Dim strFilePath As String
  2.  
  3. strFilePath = "D:\imageDogs\ChestertheDog.jpg"        'Absolute File Path
  4.  
  5. 'File Name with Extension (.jpg)
  6. strFilePath = Right$(strFilePath, Len(strFilePath) - InStrRev(strFilePath, "\"))strFilePath = Left$(strFilePath, Len(strFilePath) - 4)    ' Base File Name
  7.    Debug.Print strFilePath     'see OUTPUT below
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. ChestertheDog
Hi ADezii,

Thanks so much. I'll give this a try. I've been working on the previous one, but I wasn't getting the results I needed. I still need the full paths to be stored, but only view a portion of the string in the control window. Is that what yours does?I can't lose the actual path because my command buttons (previous & next) for search through the images is based on that.

How would I declare strFilePath since it will change with every end user once I put this out on server? There will be an absolute path, but it won't be hardcoded.

The control name that will display name is called textdescription. Control is based on the 'imageFile'....which displays (in subform) the actual image picture and show the absolute path in the current 'textdescription window. That path is stored in db so end-users can search. thanks so much.
Jun 8 '07 #6

imrosie
100+
P: 222
Hi



Does this help ?

Expand|Select|Wrap|Line Numbers
  1. Sub test()
  2.     Dim Temp As String
  3.     Dim FName As String
  4.     Dim Ext As String
  5.     Temp = "D:\imageDogs\ChestertheDog.jpg"
  6.  
  7.     Ext = Mid(Temp, InStrRev(Temp, ".") + 1)
  8.  
  9.     FName = Left(Temp, InStrRev(Temp, ".") - 1)
  10.     FName = Mid(FName, InStrRev(Temp, "\") + 1)
  11.  
  12.     MsgBox "File Name = " & FName
  13.     MsgBox "Extention = " & Ext
  14.  
  15. End Sub
  16.  
MTB
Hi MiketheBike,

Thanks for your suggestion. Will this give me the name only in the control window, with the full path stored in the db? I have a variable called imageFile (basis of display of the actual image file in subform window and shows full path name in the form control called textdescription). Is the full path retained in db with your code or do I lose the full path? I only wanted to cut off the unneccessary part of the name in the form. thanks for your help.

Rosie
Jun 8 '07 #7

ADezii
Expert 5K+
P: 8,750
Hi ADezii,

Thanks so much. I'll give this a try. I've been working on the previous one, but I wasn't getting the results I needed. I still need the full paths to be stored, but only view a portion of the string in the control window. Is that what yours does?I can't lose the actual path because my command buttons (previous & next) for search through the images is based on that.

How would I declare strFilePath since it will change with every end user once I put this out on server? There will be an absolute path, but it won't be hardcoded.

The control name that will display name is called textdescription. Control is based on the 'imageFile'....which displays (in subform) the actual image picture and show the absolute path in the current 'textdescription window. That path is stored in db so end-users can search. thanks so much.
Assumptions:
  1. The Bound Field [TextDescription] displays the Absolute Path to the File.
  2. The newly created Field [txtBaseFile] will display the Base Filename only without the .jpg extension.

Instructions:
  1. Copy and Paste the Function below into your Form's Code Module.
    Expand|Select|Wrap|Line Numbers
    1. Private Function fParseFilePath(strFilePath As String) As String
    2.  
    3. 'File Name with Extension (.jpg)
    4. strFilePath = Right$(strFilePath, Len(strFilePath) - InStrRev(strFilePath, "\"))
    5. strFilePath = Left$(strFilePath, Len(strFilePath) - 4)    ' Base File Name
    6.    fParseFilePath = strFilePath     'see OUTPUT below
    7. End Function
  2. Place this code in your Form's Current() Event.
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Form_Current()
    2.   If IsNull(Me![TextDescription]) Then Exit Sub
    3.  
    4.   Me![txtBaseFile] = fParseFilePath(Me![TextDescription])
    5. End Sub
  3. For each Record, the Base Filename will now be displayed in the Unboound Text Box Control txtBaseFile.
Jun 8 '07 #8

imrosie
100+
P: 222
Assumptions:
  1. The Bound Field [TextDescription] displays the Absolute Path to the File.
  2. The newly created Field [txtBaseFile] will display the Base Filename only without the .jpg extension.

Instructions:
  1. Copy and Paste the Function below into your Form's Code Module.
    Expand|Select|Wrap|Line Numbers
    1. Private Function fParseFilePath(strFilePath As String) As String
    2.  
    3. 'File Name with Extension (.jpg)
    4. strFilePath = Right$(strFilePath, Len(strFilePath) - InStrRev(strFilePath, "\"))
    5. strFilePath = Left$(strFilePath, Len(strFilePath) - 4)    ' Base File Name
    6.    fParseFilePath = strFilePath     'see OUTPUT below
    7. End Function
  2. Place this code in your Form's Current() Event.
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Form_Current()
    2.   If IsNull(Me![TextDescription]) Then Exit Sub
    3.  
    4.   Me![txtBaseFile] = fParseFilePath(Me![TextDescription])
    5. End Sub
  3. For each Record, the Base Filename will now be displayed in the Unboound Text Box Control txtBaseFile.
I'm not clear Do you mean on the main form control module?,,, not an event for the actual control? for pasting the funtion. I have two base Modules; 1 for opening the file and 1 for loading the images...I put them in both modules, 1 at a time and I get this error that it doesn't recognize the fParseFilePath(Me![TextDescription]). So I'm not sure where to put it; here are 2 modules:

BaseFileOpen: Option Compare Database
Option Explicit

' Code to display standard "Open File" dialog.

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

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

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

Private Const ALLFILES = "All files"

Function MakeFilterString(ParamArray varFilt() As Variant) As String
' Create filter string.
' Returns "" if there are no arguments.
' Expects an even number of arguments (filter name, extension).
' Adds *.* if the number of arguments is odd.

Dim strFilter As String
Dim intRes As Integer
Dim intNum As Integer

intNum = UBound(varFilt)
If (intNum <> -1) Then
For intRes = 0 To intNum
strFilter = strFilter & varFilt(intRes) & vbNullChar
Next
If intNum Mod 2 = 0 Then
strFilter = strFilter & "*.*" & vbNullChar
End If

strFilter = strFilter & vbNullChar
End If

MakeFilterString = strFilter
End Function

Private Sub InitOFN(OFN As OPENFILENAME)
With OFN
' Initialize fields user doesn't want to know about
.hwndOwner = hWndAccessApp
.hInstance = 0
.lpstrCustomFilter = vbNullString
.nMaxCustFilter = 0
.lpfnHook = 0
.lpTemplateName = 0
.lCustData = 0
.nMaxFile = 511
.lpstrFileTitle = String(512, vbNullChar)
.nMaxFileTitle = 511
.lStructSize = Len(OFN)
' Use default filter if not specified.
If .lpstrFilter = "" Then
.lpstrFilter = MakeFilterString(ALLFILES)
End If
' Pad lpstrFile with null chars.
.lpstrFile = .lpstrFile & String(512 - Len(.lpstrFile), vbNullChar)
End With
End Sub

Function OpenDialog(OFN As OPENFILENAME) As Boolean
Dim intRes As Integer
InitOFN OFN
intRes = GetOpenFileName(OFN)
If intRes Then
' Remove trailing null chars from lpstrFile.
With OFN
.lpstrFile = Left$(.lpstrFile, InStr(.lpstrFile, vbNullChar) - 1)
End With
End If
OpenDialog = intRes
End Function

BaseLoadImage:
Option Compare Database
Option Explicit

Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Public Function OpenFile(sFileName As String)
On Error GoTo Err_OpenFile

OpenFile = ShellExecute(Application.hWndAccessApp, "Open", sFileName, "", "C:\", 1)

Exit_OpenFile:
Exit Function

Err_OpenFile:
MsgBox Err.Number & " - " & Err.Description
Resume Exit_OpenFile

End Function
Jun 8 '07 #9

imrosie
100+
P: 222
Hello and apologies ADezii....

I finally got it working as you designed.....there is one little issue....when I select a file, it give me an error on some files...."invalid use of a null"....
When this happens, it totally breaks the control with the previous file name stuck in there, and I have to close the form and reopen to start again.....How can I fix this? thanks

Rosie
Jun 8 '07 #10

ADezii
Expert 5K+
P: 8,750
Hello and apologies ADezii....

I finally got it working as you designed.....there is one little issue....when I select a file, it give me an error on some files...."invalid use of a null"....
When this happens, it totally breaks the control with the previous file name stuck in there, and I have to close the form and reopen to start again.....How can I fix this? thanks

Rosie
Rosie, I need to know exactly where and when this Error is occurring.
Jun 8 '07 #11

imrosie
100+
P: 222
Rosie, I need to know exactly where and when this Error is occurring.
Hello ADezii,

I went back to a backup copy and started all over again inserting your code. What's happening now is the in the 'Form Current' it gives the compile error "Sub or Function" not defined.....So at this point nothing is working.

I have 2 base Modules, 1 for the loading of the images and one for opening files (general declarations). I pasted it in the bottom of the 2nd module:

Option Compare Database
Option Explicit

' Code to display standard "Open File" dialog.

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

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

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

Private Const ALLFILES = "All files"

Function MakeFilterString(ParamArray varFilt() As Variant) As String
' Create filter string.
' Returns "" if there are no arguments.
' Expects an even number of arguments (filter name, extension).
' Adds *.* if the number of arguments is odd.

Dim strFilter As String
Dim intRes As Integer
Dim intNum As Integer

intNum = UBound(varFilt)
If (intNum <> -1) Then
For intRes = 0 To intNum
strFilter = strFilter & varFilt(intRes) & vbNullChar
Next
If intNum Mod 2 = 0 Then
strFilter = strFilter & "*.*" & vbNullChar
End If

strFilter = strFilter & vbNullChar
End If

MakeFilterString = strFilter
End Function

Private Sub InitOFN(OFN As OPENFILENAME)
With OFN
' Initialize fields user doesn't want to know about
.hwndOwner = hWndAccessApp
.hInstance = 0
.lpstrCustomFilter = vbNullString
.nMaxCustFilter = 0
.lpfnHook = 0
.lpTemplateName = 0
.lCustData = 0
.nMaxFile = 511
.lpstrFileTitle = String(512, vbNullChar)
.nMaxFileTitle = 511
.lStructSize = Len(OFN)
' Use default filter if not specified.
If .lpstrFilter = "" Then
.lpstrFilter = MakeFilterString(ALLFILES)
End If
' Pad lpstrFile with null chars.
.lpstrFile = .lpstrFile & String(512 - Len(.lpstrFile), vbNullChar)
End With
End Sub

Function OpenDialog(OFN As OPENFILENAME) As Boolean
Dim intRes As Integer
InitOFN OFN
intRes = GetOpenFileName(OFN)
If intRes Then
' Remove trailing null chars from lpstrFile.
With OFN
.lpstrFile = Left$(.lpstrFile, InStr(.lpstrFile, vbNullChar) - 1)
End With
End If
OpenDialog = intRes
End Function

Private Function fParseFilePath(strFilePath As String) As String

'File Name with Extension (.jpg or .gif, etc.)
strFilePath = Right$(strFilePath, Len(strFilePath) - InStrRev(strFilePath, "\"))
strFilePath = Left$(strFilePath, Len(strFilePath) - 4) ' Base File Name
fParseFilePath = strFilePath
End Function

Should I have 'Private Declare Functon' for fParse? thanks
Jun 9 '07 #12

imrosie
100+
P: 222
Hi MiketheBike,

Thanks for your suggestion. Will this give me the name only in the control window, with the full path stored in the db? I have a variable called imageFile (basis of display of the actual image file in subform window and shows full path name in the form control called textdescription). Is the full path retained in db with your code or do I lose the full path? I only wanted to cut off the unneccessary part of the name in the form. thanks for your help.

Rosie
Hi Mike, I pasted this into the control.....it does not work..Do you have another suggestion? thanks

Rosie
Jun 14 '07 #13

ADezii
Expert 5K+
P: 8,750
Hello ADezii,

I went back to a backup copy and started all over again inserting your code. What's happening now is the in the 'Form Current' it gives the compile error "Sub or Function" not defined.....So at this point nothing is working.

I have 2 base Modules, 1 for the loading of the images and one for opening files (general declarations). I pasted it in the bottom of the 2nd module:

Option Compare Database
Option Explicit

' Code to display standard "Open File" dialog.

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

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

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

Private Const ALLFILES = "All files"

Function MakeFilterString(ParamArray varFilt() As Variant) As String
' Create filter string.
' Returns "" if there are no arguments.
' Expects an even number of arguments (filter name, extension).
' Adds *.* if the number of arguments is odd.

Dim strFilter As String
Dim intRes As Integer
Dim intNum As Integer

intNum = UBound(varFilt)
If (intNum <> -1) Then
For intRes = 0 To intNum
strFilter = strFilter & varFilt(intRes) & vbNullChar
Next
If intNum Mod 2 = 0 Then
strFilter = strFilter & "*.*" & vbNullChar
End If

strFilter = strFilter & vbNullChar
End If

MakeFilterString = strFilter
End Function

Private Sub InitOFN(OFN As OPENFILENAME)
With OFN
' Initialize fields user doesn't want to know about
.hwndOwner = hWndAccessApp
.hInstance = 0
.lpstrCustomFilter = vbNullString
.nMaxCustFilter = 0
.lpfnHook = 0
.lpTemplateName = 0
.lCustData = 0
.nMaxFile = 511
.lpstrFileTitle = String(512, vbNullChar)
.nMaxFileTitle = 511
.lStructSize = Len(OFN)
' Use default filter if not specified.
If .lpstrFilter = "" Then
.lpstrFilter = MakeFilterString(ALLFILES)
End If
' Pad lpstrFile with null chars.
.lpstrFile = .lpstrFile & String(512 - Len(.lpstrFile), vbNullChar)
End With
End Sub

Function OpenDialog(OFN As OPENFILENAME) As Boolean
Dim intRes As Integer
InitOFN OFN
intRes = GetOpenFileName(OFN)
If intRes Then
' Remove trailing null chars from lpstrFile.
With OFN
.lpstrFile = Left$(.lpstrFile, InStr(.lpstrFile, vbNullChar) - 1)
End With
End If
OpenDialog = intRes
End Function

Private Function fParseFilePath(strFilePath As String) As String

'File Name with Extension (.jpg or .gif, etc.)
strFilePath = Right$(strFilePath, Len(strFilePath) - InStrRev(strFilePath, "\"))
strFilePath = Left$(strFilePath, Len(strFilePath) - 4) ' Base File Name
fParseFilePath = strFilePath
End Function

Should I have 'Private Declare Functon' for fParse? thanks
Rosie, refer to Post 9, Item #1 under the Instructions Heading. If this is somehow causing you problems. copy and paste the Function code into 1 of your 'Base' Modules but preface it with Public as in the code listing below. This Function will now be 'Global' and can be referenced from anywhere within your Application. Let me know how you make out:
Expand|Select|Wrap|Line Numbers
  1. Public Function fParseFilePath(strFilePath As String) As String
  2.   'File Name with Extension (.jpg)
  3.   strFilePath = Right$(strFilePath, Len(strFilePath) - InStrRev(strFilePath, "\"))
  4.   strFilePath = Left$(strFilePath, Len(strFilePath) - 4)    ' Base File Name
  5.     fParseFilePath = strFilePath    
  6. End Function
Jun 14 '07 #14

imrosie
100+
P: 222
Rosie, refer to Post 9, Item #1 under the Instructions Heading. If this is somehow causing you problems. copy and paste the Function code into 1 of your 'Base' Modules but preface it with Public as in the code listing below. This Function will now be 'Global' and can be referenced from anywhere within your Application. Let me know how you make out:
Expand|Select|Wrap|Line Numbers
  1. Public Function fParseFilePath(strFilePath As String) As String
  2.   'File Name with Extension (.jpg)
  3.   strFilePath = Right$(strFilePath, Len(strFilePath) - InStrRev(strFilePath, "\"))
  4.   strFilePath = Left$(strFilePath, Len(strFilePath) - 4)    ' Base File Name
  5.     fParseFilePath = strFilePath    
  6. End Function
Thanks so much for this.....I'm goihg to try this out now..I'll get back to you with the results....

Rosie
Jun 15 '07 #15

ADezii
Expert 5K+
P: 8,750
Thanks so much for this.....I'm goihg to try this out now..I'll get back to you with the results....

Rosie
Keep me posted, Rosie.
Jun 15 '07 #16

imrosie
100+
P: 222
Keep me posted, Rosie.
Hello ADezii,

IT works, I must have put it in the wrong module before, or maybe now that it's global that fixed it, but it does work! Major hurdle to overcome, if you can imagine.
One slight problem, when I browse and select a new image, the last image name is still in 'txtBaseFile', until I save the new image, then move to 'previous', then forward to see the right name associated with the new image. Should I move code from the Form Current event to clear this up? I'll try another event while waiting for your response.

thanks for your major help...

Rosie
Jun 15 '07 #17

ADezii
Expert 5K+
P: 8,750
Hello ADezii,

IT works, I must have put it in the wrong module before, or maybe now that it's global that fixed it, but it does work! Major hurdle to overcome, if you can imagine.
One slight problem, when I browse and select a new image, the last image name is still in 'txtBaseFile', until I save the new image, then move to 'previous', then forward to see the right name associated with the new image. Should I move code from the Form Current event to clear this up? I'll try another event while waiting for your response.

thanks for your major help...

Rosie
Did you follow the code in Post #8, Item #2 under Instructions?
Jun 15 '07 #18

imrosie
100+
P: 222
Did you follow the code in Post #8, Item #2 under Instructions?
Yes ADezii, to the letter. However, I have an event that complains when I recompiled after adding your code. It's the event (using the recordset) matches the imageID to the image. I get a gray bar over the 'Str' in the rs.FindFirst line. I
am too much of a newbie to know how exactly to fix this one.

Private Sub Combo48_AfterUpdate()
' Find the record that matches the control.
Dim rs As Object
Set rs = Me.Recordset.Clone
rs.FindFirst "[imageID] = " & Str(Me![Combo48])
Me.Bookmark = rs.Bookmark
End Sub

Here's my 'Private Sub Form_Current code as you directed (your code is bolded):

Private Sub Form_Current()
On Error GoTo HandleErr

Me.RecordsetClone.MoveLast

If (Me.RecordsetClone.RecordCount) < 1.5 Then
Me.cmdNext.Enabled = False
Me.cmdPrev.Enabled = False

ElseIf Me.RecordsetClone.RecordCount = Me.CurrentRecord Then
Me.cmdNext.Enabled = False
Me.cmdPrev.Enabled = True

ElseIf Me.CurrentRecord = 1 Then
Me.cmdNext.Enabled = True
Me.cmdPrev.Enabled = False

Else
Me.cmdNext.Enabled = True
Me.cmdPrev.Enabled = True

End If

If Not IsNull([imageFile]) Then
Forms!frmimageInventory!frmimagesubform![imgPicture].Picture = [imageFile]
SysCmd acSysCmdSetStatus, "Image: '" & [imageFile] & "'."
Else
Forms!frmimageInventory!frmimagesubform![imgPicture].Picture = ""
SysCmd acSysCmdClearStatus
End If
If IsNull(Me![txtDescription]) Then Exit Sub

Me![txtBaseFile] = fParseFilePath(Me![txtDescription])

Exit Sub
HandleErr:
If Err = 2220 Then
Forms!frmimageInventory!frmimagesubform![imgPicture].Picture = ""
SysCmd acSysCmdSetStatus, "Can't open image: '" & [imageFile] & "'"
Else
MsgBox Err.Description, vbExclamation
End If
End Sub

I'm also getting an error regarding this 'Add-in'....I don't know where it's coming from [4tdocm10] . Have you seen this before???
Thanks
Rosie
Jun 17 '07 #19

ADezii
Expert 5K+
P: 8,750
Yes ADezii, to the letter. However, I have an event that complains when I recompiled after adding your code. It's the event (using the recordset) matches the imageID to the image. I get a gray bar over the 'Str' in the rs.FindFirst line. I
am too much of a newbie to know how exactly to fix this one.

Private Sub Combo48_AfterUpdate()
' Find the record that matches the control.
Dim rs As Object
Set rs = Me.Recordset.Clone
rs.FindFirst "[imageID] = " & Str(Me![Combo48])
Me.Bookmark = rs.Bookmark
End Sub

Here's my 'Private Sub Form_Current code as you directed (your code is bolded):

Private Sub Form_Current()
On Error GoTo HandleErr

Me.RecordsetClone.MoveLast

If (Me.RecordsetClone.RecordCount) < 1.5 Then
Me.cmdNext.Enabled = False
Me.cmdPrev.Enabled = False

ElseIf Me.RecordsetClone.RecordCount = Me.CurrentRecord Then
Me.cmdNext.Enabled = False
Me.cmdPrev.Enabled = True

ElseIf Me.CurrentRecord = 1 Then
Me.cmdNext.Enabled = True
Me.cmdPrev.Enabled = False

Else
Me.cmdNext.Enabled = True
Me.cmdPrev.Enabled = True

End If

If Not IsNull([imageFile]) Then
Forms!frmimageInventory!frmimagesubform![imgPicture].Picture = [imageFile]
SysCmd acSysCmdSetStatus, "Image: '" & [imageFile] & "'."
Else
Forms!frmimageInventory!frmimagesubform![imgPicture].Picture = ""
SysCmd acSysCmdClearStatus
End If
If IsNull(Me![txtDescription]) Then Exit Sub

Me![txtBaseFile] = fParseFilePath(Me![txtDescription])

Exit Sub
HandleErr:
If Err = 2220 Then
Forms!frmimageInventory!frmimagesubform![imgPicture].Picture = ""
SysCmd acSysCmdSetStatus, "Can't open image: '" & [imageFile] & "'"
Else
MsgBox Err.Description, vbExclamation
End If
End Sub

I'm also getting an error regarding this 'Add-in'....I don't know where it's coming from [4tdocm10] . Have you seen this before???
Thanks
Rosie
Expand|Select|Wrap|Line Numbers
  1. If [ImageID] is a Numeric Field, then this line of code should read:
  2. rs.FindFirst "[imageID] = " & Me![Combo48]
  3.  
  4. If [ImageID] is a String Field, then this line of code should read:
  5. rs.FindFirst "[imageID] = '" & Me![Combo48] & "'"
Jun 17 '07 #20

imrosie
100+
P: 222
Expand|Select|Wrap|Line Numbers
  1. If [ImageID] is a Numeric Field, then this line of code should read:
  2. rs.FindFirst "[imageID] = " & Me![Combo48]
  3.  
  4. If [ImageID] is a String Field, then this line of code should read:
  5. rs.FindFirst "[imageID] = '" & Me![Combo48] & "'"
Thanks ADezii,

I added your code and changed a couple things, now this works great. Thanks so much

Rosie
Jun 18 '07 #21

Post your reply

Sign in to post your reply or Sign up for a free account.