Image Change based on Combo box selection on form

mba_110

Registered User.
Local time
Yesterday, 19:25
Joined
Jan 20, 2015
Messages
280
Dear All,

I have been looking some information on changing image based on form combo box selection on form.

I manage to do case by case but i need it in a simple code because their will be many employees just to avoid adding case by case code for each one.

Code:
Private Sub Emp_IDCombo_AfterUpdate()
Select Case Emp_IDCombo.Value
Case "AM-001"
Imageholder.Picture = "C:\Users\AMG\Desktop\am-001.jpg"
Case "AM-002"
Imageholder.Picture = "C:\Users\AMG\One Documents\HR & Admin Database\Employee Pictures\am-002.jpg"
End Select
Code:


I have employees table where all images location is saved in text field and i have a combo box on form which is employee id.

Tables relationship
Employees_table [PK] to Contracts_table [FK] via field name {emp_ID}

Fields Name
Combo Box name on form Emp_IDCombo and row source is SELECT Employees_table.Emp_ID, Employees_table.EmployeeName, Employees_table.Emp_Pics FROM Employees_table;

Text field is located in employees_table called [Emp_Pic] for images location.

Please if you can provide any help it will be great.

thanks & regards,

MBA
 
Hi,

I assume you don't know SQL ;) If so, I recommend you learn, google SQL tutorial, there are some great free courses out there.

until then, you can use Dlookup function: http://www.techonthenet.com/access/functions/domain/dlookup.php

Your mind is set rite: we tend to avoid hard-coding, rather save and select our data from the DB.

read up, and come back with question if you have to clarify something.

ATB
 
Many thnks Marlan for your advice.

I have seen the Dlookup details on your link provided above but my question is, do i have to use Dlookup function for this image control issue? yes you are write i am new and have zero knowledge of programming but manage to accomplished a small project of HR data base using examples and videos online i am not a programmer but like to complete my progress which i have started couple of week ago for that i post this thread here.

Please i will definitely look at free courses online but for now i need the code to finalize the task.

thanks & regards,

MBA
 
Hi,

emp_id is numeric? DLookup("FieldName" , "TableName" , "Criteria = n")
just fill in the name of your table, and field holding the path to the picture.

your Criteria section should be something like
Code:
 "emp_ID = " & Me.Emp_IDCombo.Value

now you can sign the result of the above to Imageholder.Picture.

try it, let us know what are the results

Good luck!
 
Many Thnks marlan

I have used the above method and recorded in afterupdate event in combo box on form.

Private Sub Emp_IDCombo_AfterUpdate()
Imageholder.Picture = DLookup("[emp_Pic]", "Employees_table", "[Emp_ID]='" & Me.Emp_IDCombo.Value)

End Sub
Emp_ID field in employees_table is text field so i did little modification on this but its showing me error.

run-time error '3075':

Syntax error in string in query expression '[emp_ID] = 'AM-001'.

Actually AM-001 is employees number which is text field and i dont really understand the error is their anything wrong with coding?

Please help.

regards,

MBA
 
the value of emp_ID has to be enclosed in quotes on both sides:
Code:
Imageholder.Picture = DLookup("[emp_Pic]", "Employees_table", "[Emp_ID]='" & Me.Emp_IDCombo.Value & "'")
 
Many thanks Marlan this is now working but their is one problem if i don't have picture for any record its giving me the message.

run-time error '94':

Invalid use of null

and also if no data being selected in my combo box than image holder should not show any picture but here in my form its showing me the last record's picture i have selected.

for example i select employee ID AM-001 than its showing me the pic of that employee if i select employee ID AM-002 in combo box then its giving me the above message as null because i dont have image for AM-002 and showing me the last image which is AM-001 i have selected previously.

I need to get rid of this no image in employees record field than should not show any image of previous selections should appear as blank in image holder and no message appear for null values.

Please help on this also.

regards,

MBA
 
Last edited:
Hi,
then check the result of Dlookup for Nulls before setting it in the control. You can use a temporary variable, and/or Nz() function for that.

See if in case of null you can set the picture to ""

ATB
 
Got it marlan thanks i have solved the issue.
 

Users who are viewing this thread

Back
Top Bottom