Help with form load and set focus

Hello, I'm attempting to populate a picture on a form load event. My code is failing when I try to set focus to another control on the form once I get the value I need from the first control I setFocus on.

What am I doing wrong? The error is: You can't move to a previous control when only one control has had the focus.

Here is my code:
Private Sub Form_Load()

Dim stDocName As String

    DoCmd.Echo False
    
    DoCmd.Maximize
    
    Dim Picture1, Picture2, Picture3, File1, File2, File3 As String
    
    Me.txt_Picture1.Visible = True
    Me.txt_Picture1.SetFocus
    File1 = Me.txt_Picture1.Text
    Screen.PreviousControl.SetFocus
    Me.txt_Picture1.Visible = False
    Me.Picture1.Picture = (GetDefaultFilePath & File1)
    
    DoCmd.Echo True
    
End Sub

Open in new window

gogetsomeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TamilvananrajaCommented:
///  Me.txt_Picture1.SetFocus  

in form load event, kindly use tabindex=0 for set focus.

me.txt_picture1.tabindex =0

Good Luck..
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dale FyeCommented:
1.  Unlike Visual Basic and some other programming languages, you cannot use this syntax:

 Dim Picture1, Picture2, Picture3, File1, File2, File3 As String

To declare all of those variables as strings.  The only one of those variables that will be interpreted as a string in VBA is File3; the rest will all be variants.  You must declare each variable independently.

Dim Picture1 as string, Picture2 as string, Picture3 As String
Dim File1 As String, File2 As String, File3 As String

2.  I'm also unsure why you think you need to change the visible property of the txt_Picture control or keep changing the focus.  Get rid of lines 10-16 (or remark them out if you prefer) and try:

Me.Picture1.Picture = (GetDefaultFilePath & me.txt_Picture1)

This of course assumes that txt_Picture1 contains a full file name that points to an image.

I can understand why you were using Docmd.Echo when you thought you had to change the focus and visibility, but since you have now removed all of that excess code, you can also remove both of the Docmd.Echo lines

3.  If you are going to use docmd.Echo to stop and restart screen painting, then you need to make sure the procedures have very good error handling, something like:

Private Sub Dosomething

    On Error Goto ProcError

     docmd.Echo False

'your other code here

ProcExit:
    docmd.Echo True
    Exit Sub
ProcError:
    'code to handle your error
    Resume ProcExit

End sub

by doing this, you ensure that after you handle your error, it goes to ProcExit, which is where the docmd.Echo True is located.  If you fail to use syntax similar to this and your procedure raises an error, your screen will not refresh and the only way to close your application will be with Task Manager (not a good thing).
0
gogetsomeAuthor Commented:
Fyed, thank you for your detailed response. The reason I was showing/hiding and setting focus is because when I tried to assign

Me.Picture1.Picture = (GetDefaultFilePath & me.txt_Picture1)

I would get an error that I could not access a control that does not have focus.

The txt_Picture one is not visible on the form and contains the image filename. I do not want to show that to the user. When I try to access the value of txt_picture1 I get an error that is has to be visible or something like that...

Is there a better way to accessing record elements other than accessing them from a control after it has been populated?

Sorry for the newb questions and code.
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

gogetsomeAuthor Commented:
Okay, I've read some and have made these changes:
Private Sub Form_Load()

    Dim stDocName As String
    
    DoCmd.Maximize
    
    DefaultFilePath = GetDefaultFilePath
    If Me!txt_Picture1 > "" Then
    Me!Picture1.Picture = DefaultFilePath & Me!txt_Picture1
    End If
    If Me!txt_Picture2 > "" Then
    Me!Picture2.Picture = DefaultFilePath & Me!txt_Picture2
    End If
    If Me!txt_Picture3 > "" Then
    Me!Picture3.Picture = DefaultFilePath & Me!txt_Picture3
    End If
End Sub

Open in new window


Which seems to work a lot better. Is that more in line with the way access works?
0
Dale FyeCommented:
That should probably work, but is not as detailed as what it originally appeared that you wanted to do, which seemed to be that you wanted to hide the picture controls if the textbox defining the path to the file was blank.

Does this do what you wanted?
0
gogetsomeAuthor Commented:
It does accomplish my task. This code works on my form but the same code does not work on a report of the same record. What is the difference between a form and a report?

Private Sub Report_Open(Cancel As Integer)

DoCmd.Maximize

DefaultFilePath = GetDefaultFilePath
    If Me!txt_Picture1 > "" Then
    Me!Picture1.Picture = DefaultFilePath & Me!txt_Picture1
    End If
    If Me!txt_Picture2 > "" Then
    Me!Picture2.Picture = DefaultFilePath & Me!txt_Picture2
    End If
    If Me!txt_Picture3 > "" Then
    Me!Picture3.Picture = DefaultFilePath & Me!txt_Picture3
    End If
    
End Sub

Open in new window

0
gogetsomeAuthor Commented:
Okay, I believe I found the difference... You can't do that in the Report_Open but can in the Detail_Format.

Problem solved.

I have another question but am going to post a new question. :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.