Using code with more than one control (1 Viewer)

adambedford

Registered User.
Local time
Today, 12:07
Joined
Apr 15, 2009
Messages
42
Hi,

I have some code to open a browse window and insert the file path & name into a control.

Is there a way of using the same code for different instances of the browse i.e. from different buttons with different controls. Essentially, I need there to be 6 sets of button and text controls all calling the browse function.

The application is for photo browsing and once the file has been selected, the path is inserted into a text control with is linked to a filed in a corresponding table. The code also displays the image and displays an error if there is no file.

I have attached a copy of the database showing the current situation
Any help would be greatly appreciated.

Adam

View attachment PhotoBrowseDB.zip
 

DCrake

Remembered
Local time
Today, 11:07
Joined
Jun 8, 2005
Messages
8,632
Couple of tips

Usiing the GetFileName sub routine you could pass the sub a number which corresponds to the image control name, such as

Call getFileName(3) where three relates to ImagePath3

In your sub you can use a select case statement to evaluate the value and pass the details to the relevant imagecontrol

Code:
Sub getFileName(Target As Integer)

....

        If (result <> 0) Then
             fileName = Trim(.SelectedItems.Item(1))           
             Select Case Target
                 Case 1

                       Me![ImagePath1].Visible = True
                       Me![ImagePath1].SetFocus            '
                 case 2
                       .....
                 .....
             End Select
        End If


So on the OnClick of each button enter

Call getFileName(n) where n represents the image number

Also if you use InStrRev in your ExtractName Function it will perform quicker


ExtractName = Mid(FileName,InStrRev(FileName,"\")+1)

Similar to InStr, InStrRev reads from right to left


David
 

adambedford

Registered User.
Local time
Today, 12:07
Joined
Apr 15, 2009
Messages
42
Thank you very much...I'm in the process of implementing that right now.

One other question: How would I get the error message to work with different instances? At the moment, the error message is a label placed over the image location and set to visible if there is an error. I plan to have an error label over each photo.

Thanks
 

DCrake

Remembered
Local time
Today, 11:07
Joined
Jun 8, 2005
Messages
8,632
Why not place your error behind the image control and if the image control is invisible it automatically displays the "No Image available" caption.
 

adambedford

Registered User.
Local time
Today, 12:07
Joined
Apr 15, 2009
Messages
42
OK thanks...

I'm quite new at this so...how would I go about that?

Sorry for so many questions!
 

adambedford

Registered User.
Local time
Today, 12:07
Joined
Apr 15, 2009
Messages
42
Hi,

I have one other question!

At the moment in the database, the function ExtractName removes the path from the full name, but I would like to be able to store images in folders within the database folder so how would I strip just the CurrentProject.Path and would doing this enable images to be stored in folders beneath the database folder?

Thanks
 

DCrake

Remembered
Local time
Today, 11:07
Joined
Jun 8, 2005
Messages
8,632
Ok
Lets say the file name = C:\Program files\Projects\Villas\PhotoBrowse.mdb

We know that the CurrentProject.Path = C:\Program files\Projects\Villas

We know we store all the images in a subfolder of the project path called Images

C:\Program files\Projects\Villas\Images

So the location of the photos are stored in

CurrentProject.Path & "\Images"

So when using the filesearch we can specify the initial folder as being

CurrentProject.Path & "\Images"

Re you other question

Add a label for each image control on top of the imagecontrol as you have done for the first image.
You could say after your select statement

Me.NoPicture1.Visible = Not Me.Image1.Visible

So if image1 has a picture and is visible then NoPicture is invisible. By using the NOT keyword it will always be the opposite value to the image control.

David


Me.
 

jardiamj

Registered User.
Local time
Today, 03:07
Joined
Apr 15, 2009
Messages
59
Hello Adam!
I checked your DB and I found why it was giving you the error, the error was in the Sub ImagePath1_AfterUpdate() in this part of the code:

Code:
 showErrorMessage(errormsg)

This function that you created doesn't accept any variable, so I just remove the (errormsg) from this line and leave it just like this:
Code:
 showErrorMessage

But I still don't understand what you want this function ShowErrorMessage to do, as I couldn't figure out the functionality you are trying to give to it by the code.
I hope you can understand my English, I need to study more... lol.
If you tell what you are trying to do with the ShowErrorMessage function I'm always willing to help if I can, and if my girlfriend gives me time... lol.
Have a nice day. Cheers!
 

Attachments

  • PhotoBrowseDB.zip
    352.5 KB · Views: 127

adambedford

Registered User.
Local time
Today, 12:07
Joined
Apr 15, 2009
Messages
42
Hi jardiamj,

I am trying to get frmVillas to display six images. Each image is controlled by a field and a "browse" button which opens the File Browser window. At the moment, I can do this, but I need to have an error messag that displays when an image cannot be located.

The Funciton showErrorMessage works for the first image, but I do not know how to get it to work with the other five.

I also need help with the relative paths (my other post) as the images will be stored in folder called Images and then another folder called [v_Name]. At the moment, my function ExtractName removes everything apart from the file name...which means it doesnt work!

Any help would be greatly appreciated!

Any your English is very good!

Adam
 

jardiamj

Registered User.
Local time
Today, 03:07
Joined
Apr 15, 2009
Messages
59
Hello Adam!, about you path and file name issue I already replied you in the other post you put here... Man you are leaving posts everywhere... lol. I agree with Dcracke you should have posted that here as well.
Any way, look if that works. And about the errorMessage, you can always pass a control or the name of a control to the function so your function can be called for any object in your application to be reusable.
I hope it helps. Cheers!
 

adambedford

Registered User.
Local time
Today, 12:07
Joined
Apr 15, 2009
Messages
42
jardiamj, I'm really confused!!

errorMessage wont let me pass a control to it. I get an error. I'm also struggling to get your relative image path code to work with frmVillas and with the 6 browse buttons.

Thank you in advance for any help!

Adam
 

jardiamj

Registered User.
Local time
Today, 03:07
Joined
Apr 15, 2009
Messages
59
Adam don't get confused my friend!... lol
I was working with access 97 the other day and for some reason it didn't let me to pass a control to the function. So what I did was to pass the form Name and Control Name to the Function as strings, and then reference the control inside the function like this:

Code:
Sub hideCtrl(frm As String, ctrl As String)
Forms(frm).Controls(ctrl).Visible = false
End Sub

Of course you will have to create the errorTrappers for this.
And does my function work or not, I didn't understand quite well if you said it doesn't work or you can't get it working for your purposes.
I hope it helps. Cheers
 

adambedford

Registered User.
Local time
Today, 12:07
Joined
Apr 15, 2009
Messages
42
I can't get my head around how to get certain snippets of code to work with more than one control. Im thinking I might need a complete restructuring of the code...but I don't know how!

Firstly there is this code that runs when the form starts up that checks for relative paths and also checks whether the path to the image has changed:

Code:
Private Sub Form_Current()
' Display the picture for the current property if the image
' exists. If the file name no longer exists or has been changed,
' set the errormsg label caption to the appropriate message.
Dim res As Boolean
Dim FName As String
 
Path = CurrentProject.Path
On Error Resume Next
errormsg.Visible = False
If Not IsNull(Me!Photo) Then
res = IsRelative(Me!Photo)
FName = Me![ImagePath1]
If (res = True) Then
FName = Path & "\" & FName
End If
 
Me![ImageFrame1].Picture = FName
showImageFrame
Me.PaintPalette = Me![ImageFrame1].ObjectPalette
If (Me![ImageFrame1].Picture <> FName) Then
hideImageFrame
errormsg.Caption = "Picture Not Found"
errormsg.Visible = True
End If
Else
hideImageFrame
errormsg.Caption = "Please browse for an image"
errormsg.Visible = True
End If
 
End Sub

And then there are several sub routines (I think they should be functions) that are related to the above code:

Code:
Function IsRelative(FName As String) As Boolean
    ' Return false if the file name contains a drive or UNC path
    IsRelative = (InStr(1, FName, ":") = 0) And (InStr(1, FName, "\\") = 0)
End Function

Code:
Sub showImageFrame()
    ' Display the image control
    Me![ImageFrame1].Visible = True
End Sub

Code:
Sub hideImageFrame()
    ' Hide the image control
    Me![ImageFrame1].Visible = False
End Sub

Code:
Sub showErrorMessage()
    ' Display the errormsg label if the image file is not available.
    If Not IsNull(Me!Photo) Then
        errormsg.Visible = False 
    Else
        errormsg.Visible = True
    End If
End Sub

Essentially what I can't figure out is how to get the code to work with six ImagePaths, six ImageFrames and six errormsgs. I also dont understand to the Form OnCurrent bit can do all this. I think I need to put the OnCurrent code into a separate function and pass it variables. But then this code needs to pass variables of its own to the four smaller functions.

Does the OnCurrent code need to loop through six times, each time setting a variable as a different control?

I'm really not good enough at VBA to write the syntax myself, but I do understand the general principles of what (I think) should be going on.

If it would help to see my database I can attach it.

Any advice?

Thanks in advance!!
 

jardiamj

Registered User.
Local time
Today, 03:07
Joined
Apr 15, 2009
Messages
59
Hello Adam!
I don't have so much time right now, so I just saw your code briefly. I think you just need one function for this. You will need to loop through all the controls in the form check if the control is an Image for example and then pass the object to the function you want to run related with that object. So showImageFrame, hideImageFrame and showErrorMessage should accept a variable to be passed to them, and that would be either a control or the name of the form and the name of the control it is evaluating.
I hope that will give you a go!...
Good Luck!, I hope I will have more time soon.
Cheers!
 
Last edited:

adambedford

Registered User.
Local time
Today, 12:07
Joined
Apr 15, 2009
Messages
42
I have tried something...but I get the error "Can't Assign to Array". Why do I get this?

Here is the code I've written - there are three arrays, one for each type of control (ImageFrame, ImagePath, ErrorMessage)

The form OnCurrent is meant to loop through the array until it reaches 6, but for some reason, I get the message above at "strImgFrame" and I assume the error is also at "strImgPath" and "strErrMsg".

Code:
Private Sub Form_Current()
 
Dim strImgFrame(1 To 6) As Variant
Dim strImgPath(1 To 6) As Variant
Dim strErrMsg(1 To 6) As Variant
Dim paramstring1 As String
Dim paramstring2 As String
Dim paramstring3 As String
Dim LoopCount As Integer
strImgFrame = Array("ImageFrame1", "ImageFrame2", "ImageFrame3", "ImageFrame4", "ImageFrame5", "ImageFrame6")
strImgPath = Array("ImagePath1", "ImagePath2", "ImagePath3", "ImagePath4", "ImagePath5", "ImagePath6")
strErrMsg = Array("ErrorMessage1", "ErrorMessage2", "ErrorMessage3", "ErrorMessage4", "ErrorMessage5", "ErrorMessage6")
For LoopCount = 1 To 6 Step 1
paramstring1 = strImgFrame(LoopCount)
paramstring2 = strImgPath(LoopCount)
paramstring3 = strErrMsg(LoopCount)
Call ShowHideImg(paramstring1, paramstring2, paramstring3)
Next
End Sub

The function ShowHideImg is below:

Code:
Function ShowHideImg(ImgFrame As String, ImgPath As String, ErrMsg As String)
    Dim res As Boolean
    Dim FName As String
    Dim strImageFrame As String
    Dim strImagePath As String
    Dim strErrMsg As String
    
    strImageFrame = ImgFrame
    strImagePath = ImgPath
    strErrorMsg = ErrMsg
    
    Path = CurrentProject.Path
    On Error Resume Next
        strErrMsg.Visible = False
        
        If Not IsNull(Me!Photo) Then
            res = IsRelative(Me!Photo)
            FName = strImagePath
            If (res = True) Then
                FName = Path & "\" & FName
            End If
            
            strImageFrame.Picture = FName
            showImageFrame (strImageFrame)
            Me.PaintPalette = strImageFrame.ObjectPalette
            If (strImageFrame.Picture <> FName) Then
                hideImageFrame (strImageFrame)
                strErrorMessage.Caption = "Picture Not Found"
                strErrorMessage.Visible = True
            End If
        Else
            hideImageFrame (strImageFrame)
            strErrorMessage.Caption = "Please browse for an image"
            strErrorMessage.Visible = True
        End If
End Function

Please can someone help me, I'm getting annoyed with so many error messages!!
 

jardiamj

Registered User.
Local time
Today, 03:07
Joined
Apr 15, 2009
Messages
59
Hello again!
Just declare the variables as variant without the limits. So the array will start with 0, and you will have to loop from 0 to 5. It looks to me like a good solution for your issue.

Code:
Private Sub Form_Current()

Dim strImgFrame As Variant
Dim strImgPath As Variant
Dim strErrMsg As Variant
Dim paramstring1 As String
Dim paramstring2 As String
Dim paramstring3 As String
Dim LoopCount As Integer
strImgFrame = Array("ImageFrame1", "ImageFrame2", "ImageFrame3", "ImageFrame4", "ImageFrame5", "ImageFrame6")
strImgPath = Array("ImagePath1", "ImagePath2", "ImagePath3", "ImagePath4", "ImagePath5", "ImagePath6")
strErrMsg = Array("ErrorMessage1", "ErrorMessage2", "ErrorMessage3", "ErrorMessage4", "ErrorMessage5", "ErrorMessage6")
For LoopCount = 0 To 5 Step 1
paramstring1 = strImgFrame(LoopCount)
paramstring2 = strImgPath(LoopCount)
paramstring3 = strErrMsg(LoopCount)
Call ShowHideImg(paramstring1, paramstring2, paramstring3)
Next
End Sub

I hope it helps, have a nice day. Cheers!
 

adambedford

Registered User.
Local time
Today, 12:07
Joined
Apr 15, 2009
Messages
42
Hi, Thanks...its coming together now!

I get another error now though and I think its because I am trying to set the property of a variable in the same way as a control. For example, can I have "Variable.Visible = True"? I know I can have "ControlName.Visible = True", but that is different isn't it?

Code:
Function ShowHideImg(ImgFrame As String, ImgPath As String, ErrMsg As String)
    Dim res As Boolean
    Dim FName As String
    Dim strImageFrame As String
    Dim strImagePath As String
    Dim strErrMsg As String
    
    strImageFrame = ImgFrame
    strImagePath = ImgPath
    strErrorMsg = ErrMsg
    
    Path = CurrentProject.Path
    On Error Resume Next
        [B]strErrorMsg[/B].Visible = False
        
        If Not IsNull(Me!Photo) Then
            res = IsRelative(Me!Photo)
            FName = strImagePath
            If (res = True) Then
                FName = Path & "\" & FName
            End If
            
            strImageFrame.Picture = FName
            showImageFrame (strImageFrame)
            Me.PaintPalette = strImageFrame.ObjectPalette
            If (strImageFrame.Picture <> FName) Then
                hideImageFrame (strImageFrame)
                strErrorMessage.Caption = "Picture Not Found"
                strErrorMessage.Visible = True
            End If
        Else
            hideImageFrame (strImageFrame)
            strErrorMessage.Caption = "Please browse for an image"
            strErrorMessage.Visible = True
        End If
End Function

The bold bit is what gives me the error message and strErrorMsg is a variable.

Thanks
 

jardiamj

Registered User.
Local time
Today, 03:07
Joined
Apr 15, 2009
Messages
59
You are just passing the name of the control to the variable not the control itself. To do so, declare your variables as objects and then pass the controls to them by doing Me.Controls(myControlName) and then you can call the properties of the objects like you normally do.

Code:
Dim strImageFrame As Object
    Dim strImagePath As Object
    Dim strErrMsg As Object
    
    strImageFrame = Me.Controls(ImgFrame)
    strImagePath = Me.Controls(ImgPath)
    strErrorMsg = Me.Controls(ErrMsg)

I hope it will help you. Cheers!
 

adambedford

Registered User.
Local time
Today, 12:07
Joined
Apr 15, 2009
Messages
42
I am still getting error messages. Could someone point out where I'm going wrong?

Code:
Private Sub Form_Current()
    
Dim strImgFrame As Variant
Dim strImgPath As Variant
Dim strErrMsg As Variant
 
Dim paramstring1 As String
Dim paramstring2 As String
Dim paramstring3 As String
 
Dim LoopCount As Integer
 
strImgFrame = Array("ImageFrame1", "ImageFrame2", "ImageFrame3", "ImageFrame4", "ImageFrame5", "ImageFrame6")

strImgPath = Array("ImagePath1", "ImagePath2", "ImagePath3", "ImagePath4", "ImagePath5", "ImagePath6")

strErrMsg = Array("ErrorMessage1", "ErrorMessage2", "ErrorMessage3", "ErrorMessage4", "ErrorMessage5", "ErrorMessage6")
 
For LoopCount = 1 To 5 Step 1
paramstring1 = strImgFrame(LoopCount)
paramstring2 = strImgPath(LoopCount)
paramstring3 = strErrMsg(LoopCount)
 
Call ShowHideImg(paramstring1, paramstring2, paramstring3)
 
Next
 
End Sub

Code:
Function ShowHideImg(ImgFrame As String, ImgPath As String, ErrMsg As String)
    Dim res As Boolean
    Dim FName As String
    
    Dim strImageFrame As Object
    Dim strImagePath As Object
    Dim strErrMsg As Object
 
    strImageFrame = Me.Controls(ImgFrame)
    strImagePath = Me.Controls(ImgPath)
    strErrorMsg = Me.Controls(ErrMsg)
    
    
    Path = CurrentProject.Path
    On Error Resume Next
        strErrMsg.Visible = False
        
        If Not IsNull(Me!Photo) Then
            res = IsRelative(Me!Photo)
            FName = strImagePath
            If (res = True) Then
                FName = Path & "\" & FName
            End If
            
            strImageFrame.Picture = FName
            showImageFrame (strImageFrame)
            Me.PaintPalette = strImageFrame.ObjectPalette
            If (strImageFrame.Picture <> FName) Then
                hideImageFrame (strImageFrame)
                strErrorMessage.Caption = "Picture Not Found"
                strErrorMessage.Visible = True
            End If
        Else
            hideImageFrame (strImageFrame)
            strErrorMessage.Caption = "Please browse for an image"
            strErrorMessage.Visible = True
        End If
End Function

I think its very nearly correct, but i keep getting the message "Object variable or With block variable not set".

Any ideas?

Thanks
 

Users who are viewing this thread

Top Bottom