Solved putting a picture in a command button (1 Viewer)

murray83

Games Collector
Local time
Today, 18:29
Joined
Mar 31, 2017
Messages
729
i have my quiz DB which is going fine and thanks to all who have helped along the way, but i think this will be my last question to do with it

have this sub which was written by people smarter than me, and its nicley commented which helps

Code:
Private Sub GetQuestion()
  Dim dbs As DAO.Database, rst As DAO.Recordset, x As Integer
  
  Set dbs = CurrentDb
  Me.QImage.Visible = False
  Me.Question.Width = 6804 'Set the full width of the question box
  'Me.GotoNextQuestion.Enabled = False
  'Me.FiftyFifty.Enabled = True
  'Get the question and choices from the tables according to the question round number and question number
  Set rst = dbs.OpenRecordset("SELECT tblQuestions.QRoundNo, tblQuestions.QuestionNo, Question, ChoiseNo, Choise, RightAnswer, ImageAttached " _
  & "FROM tblQuestions INNER JOIN tblQuestionChoice ON (tblQuestions.QuestionNo = tblQuestionChoice.QuestionNo) AND (tblQuestions.QRoundNo = tblQuestionChoice.QRoundNo) " _
  & "WHERE tblQuestions.QRoundNo=" & TempVars!QRoundNo & " AND tblQuestions.QuestionNo=" & RememberQuestionNo & ";")
  If Not rst.EOF Then 'Is some data found?
    'Yes data found, then prepare the form
    Me.Caption = "Question " & rst![QuestionNo] 'Set the form's caption
    Me.QuestionNo.Caption = Me.Caption 'Set the QuestionNo (label) caption
    Me.Question = rst![Question] 'Set the Question
    If Not IsNull(rst![ImageAttached]) Then 'Some picture is attached to the question
      Me.Question.Width = 4479 'Reduce the width of the question box so the picture box not covered some of the question
      Me.QImage.Visible = True 'Show the picture box
     Me.QImage.Picture = rst![ImageAttached] 'Put the picture in the picture box
    End If
    For x = 1 To 4 'Prepare the 4 choises button
      Me("Choise" & x).Visible = True 'Make the button visible
      Me("Choise" & x).Enabled = True 'Make the button enable
      Me("Choise" & x).Caption = rst![Choise] 'Put the choise in the button's caption
      Me("Choise" & x).Tag = rst![RightAnswer] 'Mark the button as right or wrong choise
      rst.MoveNext
    Next x
    'Me.FiftyFifty.SetFocus 'Move the focus from the 4 choises to the FiftyFifty button
  Else
    Me.Visible = False
    MsgBox ("Sorry, no more questions!") 'Question round is finish
    DoCmd.OpenForm "F_Results"
    Forms!F_Results.Refresh
    Forms!F_Results!txtSecondsTaken.Value = txtYouHaveTaken.Value
  End If
End Sub

But to the question, how would i go about altering this if i wanted the picture to appear in the command buttons, for example if i had question like so: see attached image rather then have the image appear in the top of the page ?
 

Attachments

  • 2020-05-12_163110.png
    2020-05-12_163110.png
    70.1 KB · Views: 194
  • Quiz-A-Rama - Picture in Command Button.accdb
    1.6 MB · Views: 192

theDBguy

I’m here to help
Staff member
Local time
Today, 10:29
Joined
Oct 29, 2018
Messages
21,473
Are you asking how to make the button image change using code based on certain situations? Otherwise, you should be able to assign an image to a button in design view, correct?
 

murray83

Games Collector
Local time
Today, 18:29
Joined
Mar 31, 2017
Messages
729
Are you asking how to make the button image change using code based on certain situations? Otherwise, you should be able to assign an image to a button in design view, correct?

yeah the first one, so for each question if it has a picture as the choice rather then just text it would place a picture in the command button as it does for text
 

theDBguy

I’m here to help
Staff member
Local time
Today, 10:29
Joined
Oct 29, 2018
Messages
21,473
yeah the first one, so for each question if it has a picture as the choice rather then just text it would place a picture in the command button as it does for text
Hmm, how many images are we talking about? There is no direct way to assign an image to a button without using an external file or the Image Gallery.
 

Micron

AWF VIP
Local time
Today, 13:29
Joined
Oct 20, 2018
Messages
3,478
You can set the picture property to be linked, embedded or shared (no idea what the last one means). If embedded, the pic file is stored in the db, which makes the file size larger and I don't think you can swap an embedded image in code. Not positive though. Linked, pretty sure you can.
 

murray83

Games Collector
Local time
Today, 18:29
Joined
Mar 31, 2017
Messages
729
i am using a gallery and id would say if and when it happens would just be 4 images as have 4 buttons for possible choices
 

theDBguy

I’m here to help
Staff member
Local time
Today, 10:29
Joined
Oct 29, 2018
Messages
21,473
i am using a gallery and id would say if and when it happens would just be 4 images as have 4 buttons for possible choices
Okay, if you're using the Image Gallery, then changing the button's image is as simple as assigning the "Name" assigned to the image (as seen from the Gallery) to the Picture property of the button. For example:

Me.ButtonName.Picture = "ImageName"

Hope that helps...
 

murray83

Games Collector
Local time
Today, 18:29
Joined
Mar 31, 2017
Messages
729
Okay, if you're using the Image Gallery, then changing the button's image is as simple as assigning the "Name" assigned to the image (as seen from the Gallery) to the Picture property of the button. For example:

Me.ButtonName.Picture = "ImageName"

Hope that helps...

but that wouldnt put the right picture with the correct choice, i would have to hard code it each time

PS Edit

Also the images stay when it moves to the next question
 

Attachments

  • Quiz-A-Rama - Picture in Command Button.accdb
    1.6 MB · Views: 194
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 10:29
Joined
Oct 29, 2018
Messages
21,473
but that wouldnt put the right picture with the correct choice, i would have to hard code it each time
Not necessarily. You'll have to add some logic to your code to associate a particular image to a specific question. You can also use a mapping table for that. For example, let's say Button1 needs Image1 and Button2 needs Image2, etc. Then something like:

Code:
For x = 1 To 6
    Me.Controls("Button" & x).Picture = "Image" & x
Next
Hope that helps...
 

murray83

Games Collector
Local time
Today, 18:29
Joined
Mar 31, 2017
Messages
729
Not necessarily. You'll have to add some logic to your code to associate a particular image to a specific question. You can also use a mapping table for that. For example, let's say Button1 needs Image1 and Button2 needs Image2, etc. Then something like:

Code:
For x = 1 To 6
    Me.Controls("Button" & x).Picture = "Image" & x
Next
Hope that helps...

ok ill look at that as it came up after i edited my previous post lol
 

murray83

Games Collector
Local time
Today, 18:29
Joined
Mar 31, 2017
Messages
729
Not necessarily. You'll have to add some logic to your code to associate a particular image to a specific question. You can also use a mapping table for that. For example, let's say Button1 needs Image1 and Button2 needs Image2, etc. Then something like:

Code:
For x = 1 To 6
    Me.Controls("Button" & x).Picture = "Image" & x
Next
Hope that helps...


well i tried that, many ways and had some success, it did put the picture but still put for next question as well, so i thought of an if and else so i put this

Code:
Private Sub GetQuestion()
  Dim dbs As DAO.Database, rst As DAO.Recordset, x As Integer

  Set dbs = CurrentDb
  Me.QImage.Visible = False
  Me.Question.Width = 6804 'Set the full width of the question box
  'Me.GotoNextQuestion.Enabled = False
  'Me.FiftyFifty.Enabled = True
  'Get the question and choices from the tables according to the question round number and question number
  Set rst = dbs.OpenRecordset("SELECT tblQuestionChoice.ChoisePicture, tblQuestions.QRoundNo, tblQuestions.QuestionNo, Question, ChoiseNo, Choise, RightAnswer, ImageAttached " _
  & "FROM tblQuestions INNER JOIN tblQuestionChoice ON (tblQuestions.QuestionNo = tblQuestionChoice.QuestionNo) AND (tblQuestions.QRoundNo = tblQuestionChoice.QRoundNo) " _
  & "WHERE tblQuestions.QRoundNo=" & TempVars!QRoundNo & " AND tblQuestions.QuestionNo=" & RememberQuestionNo & ";")
  If Not rst.EOF Then 'Is some data found?
    'Yes data found, then prepare the form
    Me.Caption = "Question " & rst![QuestionNo] 'Set the form's caption
    Me.QuestionNo.Caption = Me.Caption 'Set the QuestionNo (label) caption
    Me.Question = rst![Question] 'Set the Question
    If Not IsNull(rst![ImageAttached]) Then 'Some picture is attached to the question
      Me.Question.Width = 4479 'Reduce the width of the question box so the picture box not covered some of the question
      Me.QImage.Visible = True 'Show the picture box
     Me.QImage.Picture = rst![ImageAttached] 'Put the picture in the picture box
    End If
    For x = 1 To 4 'Prepare the 4 choises button
      Me("Choise" & x).Visible = True 'Make the button visible
      Me("Choise" & x).Enabled = True 'Make the button enable
   
'new bit here
   If Not IsNull(rst![ChoisePicture]) Then
      Me("Choise" & x).Picture = rst![ChoisePicture] ' To put a picture in the button - added by me as suggested by theDBguy
      Else
      Me("Choise" & x).Caption = rst![Choise] 'Put the choise in the button's caption
      End If
    
  Me("Choise" & x).Tag = rst![RightAnswer] 'Mark the button as right or wrong choise
      rst.MoveNext
    Next x
    'Me.FiftyFifty.SetFocus 'Move the focus from the 4 choises to the FiftyFifty button
  Else
    Me.Visible = False
    MsgBox ("Sorry, no more questions!") 'Question round is finish
    DoCmd.OpenForm "F_Results"
    Forms!F_Results.Refresh
    Forms!F_Results!txtSecondsTaken.Value = txtYouHaveTaken.Value
  End If
End Sub

bit which says new bit here, but it does sorta of work, if you look at the attached it puts the picture in which is fine but then next question still has a picture and no text

also one more thing how do i change the size of picture so its just a clip or cant i as its on a command button and not an image box
 

Attachments

  • Quiz-A-Rama - Picture in Command Button with if.accdb
    1.6 MB · Views: 168

theDBguy

I’m here to help
Staff member
Local time
Today, 10:29
Joined
Oct 29, 2018
Messages
21,473
well i tried that, many ways and had some success, it did put the picture but still put for next question as well, so i thought of an if and else so i put this

Code:
Private Sub GetQuestion()
  Dim dbs As DAO.Database, rst As DAO.Recordset, x As Integer

  Set dbs = CurrentDb
  Me.QImage.Visible = False
  Me.Question.Width = 6804 'Set the full width of the question box
  'Me.GotoNextQuestion.Enabled = False
  'Me.FiftyFifty.Enabled = True
  'Get the question and choices from the tables according to the question round number and question number
  Set rst = dbs.OpenRecordset("SELECT tblQuestionChoice.ChoisePicture, tblQuestions.QRoundNo, tblQuestions.QuestionNo, Question, ChoiseNo, Choise, RightAnswer, ImageAttached " _
  & "FROM tblQuestions INNER JOIN tblQuestionChoice ON (tblQuestions.QuestionNo = tblQuestionChoice.QuestionNo) AND (tblQuestions.QRoundNo = tblQuestionChoice.QRoundNo) " _
  & "WHERE tblQuestions.QRoundNo=" & TempVars!QRoundNo & " AND tblQuestions.QuestionNo=" & RememberQuestionNo & ";")
  If Not rst.EOF Then 'Is some data found?
    'Yes data found, then prepare the form
    Me.Caption = "Question " & rst![QuestionNo] 'Set the form's caption
    Me.QuestionNo.Caption = Me.Caption 'Set the QuestionNo (label) caption
    Me.Question = rst![Question] 'Set the Question
    If Not IsNull(rst![ImageAttached]) Then 'Some picture is attached to the question
      Me.Question.Width = 4479 'Reduce the width of the question box so the picture box not covered some of the question
      Me.QImage.Visible = True 'Show the picture box
     Me.QImage.Picture = rst![ImageAttached] 'Put the picture in the picture box
    End If
    For x = 1 To 4 'Prepare the 4 choises button
      Me("Choise" & x).Visible = True 'Make the button visible
      Me("Choise" & x).Enabled = True 'Make the button enable
  
'new bit here
   If Not IsNull(rst![ChoisePicture]) Then
      Me("Choise" & x).Picture = rst![ChoisePicture] ' To put a picture in the button - added by me as suggested by theDBguy
      Else
      Me("Choise" & x).Caption = rst![Choise] 'Put the choise in the button's caption
      End If
   
  Me("Choise" & x).Tag = rst![RightAnswer] 'Mark the button as right or wrong choise
      rst.MoveNext
    Next x
    'Me.FiftyFifty.SetFocus 'Move the focus from the 4 choises to the FiftyFifty button
  Else
    Me.Visible = False
    MsgBox ("Sorry, no more questions!") 'Question round is finish
    DoCmd.OpenForm "F_Results"
    Forms!F_Results.Refresh
    Forms!F_Results!txtSecondsTaken.Value = txtYouHaveTaken.Value
  End If
End Sub

bit which says new bit here, but it does sorta of work, if you look at the attached it puts the picture in which is fine but then next question still has a picture and no text

also one more thing how do i change the size of picture so its just a clip or cant i as its on a command button and not an image box
Hi. I tool a quick look at your file earlier and couldn't decipher what you wanted to happen. I see some images assigned to the buttons, but I don't get how you want them to change per question. Could you please explain that part, so I can try it out myself? For example, when I start a quiz, what images were supposed to be assigned to each button, and what should happen to those images when I go to the next question, etc. Thanks!
 

murray83

Games Collector
Local time
Today, 18:29
Joined
Mar 31, 2017
Messages
729
ok

so when the quiz Db is first opened the player puts in a name and chooses which set of questions, in this case we only have 1 set with 2 questions.

so when name and quiz set selected, press start quiz and it will get the question, display the question at the top of the questions form, and then the 4 command buttons it will change these to teh caption of the choice based on which question it is once player has selected one, be it wrong or correct it will mark that in results table and also get next question

when there is no more questions left says so, taking you to results page showing how many you got right

then from there back to start to try other sets

hope this helps

so what i would like is, the first question (in this case but any question)to show 4 pictures in the command buttons and then the player picks one and then the second question the command buttons now display a caption ( a string from the choices db ) or maybe more pictures

cheers for looking
 

theDBguy

I’m here to help
Staff member
Local time
Today, 10:29
Joined
Oct 29, 2018
Messages
21,473
ok

so when the quiz Db is first opened the player puts in a name and chooses which set of questions, in this case we only have 1 set with 2 questions.

so when name and quiz set selected, press start quiz and it will get the question, display the question at the top of the questions form, and then the 4 command buttons it will change these to teh caption of the choice based on which question it is once player has selected one, be it wrong or correct it will mark that in results table and also get next question

when there is no more questions left says so, taking you to results page showing how many you got right

then from there back to start to try other sets

hope this helps

so what i would like is, the first question (in this case but any question)to show 4 pictures in the command buttons and then the player picks one and then the second question the command buttons now display a caption ( a string from the choices db ) or maybe more pictures

cheers for looking
Okay, thanks for that. I think it's starting to make sense. However, I took a quick look at your tblQuestionChoice table. If this is where the choices for the buttons are supposed to come from, and if I understand your post correctly, you want the first set of questions to use the Choise values as the name of the images to show up on the buttons and then use the Choise values as the Caption of those buttons for the second question, correct? If so, you'll need to add something in that table to tell your code whether to use the Choise values as either the Picture or the Caption property. I don't see that right now in your table, so how would the code know to assign the value to the correct button property?
 

murray83

Games Collector
Local time
Today, 18:29
Joined
Mar 31, 2017
Messages
729
see post 11, i added an if and else to my code and the example attached to that post as well
 

theDBguy

I’m here to help
Staff member
Local time
Today, 10:29
Joined
Oct 29, 2018
Messages
21,473
see post 11, i added an if and else to my code and the example attached to that post as well
Hi. Sorry, too late. I was playing with your file before I saw your last post. Please take a look at the attached modified copy of your file. I think it does what you're asking, but it's not the best implementation I would recommend. I'll take a look at post #11 next.
 

Attachments

  • Quiz-A-Rama - Picture in Command Button.zip
    577.4 KB · Views: 193

theDBguy

I’m here to help
Staff member
Local time
Today, 10:29
Joined
Oct 29, 2018
Messages
21,473
see post 11, i added an if and else to my code and the example attached to that post as well
Okay. I took a quick look at the file you had in post #11. That's better. All you were missing was to assign .Picture="" in the Else part. Like this:

Rich (BB code):
      If Not IsNull(rst![ChoisePicture]) Then
        Me("Choise" & x).Picture = rst![ChoisePicture] ' To put a picture in the button - added by me as suggested by theDBguy
      Else
        Me("Choise" & x).Caption = rst![Choise] 'Put the choise in the button's caption
        Me("Choise" & x).Picture = ""
      End If
 

theDBguy

I’m here to help
Staff member
Local time
Today, 10:29
Joined
Oct 29, 2018
Messages
21,473
Okay. I took a quick look at the file you had in post #11. That's better. All you were missing was to assign .Picture="" in the Else part. Like this:

Rich (BB code):
      If Not IsNull(rst![ChoisePicture]) Then
        Me("Choise" & x).Picture = rst![ChoisePicture] ' To put a picture in the button - added by me as suggested by theDBguy
      Else
        Me("Choise" & x).Caption = rst![Choise] 'Put the choise in the button's caption
        Me("Choise" & x).Picture = ""
      End If
On second thought... You could probably get rid of the If/Then/Else part like this:
Rich (BB code):
    For x = 1 To 4 'Prepare the 4 choises button
      Me("Choise" & x).Visible = True 'Make the button visible
      Me("Choise" & x).Enabled = True 'Make the button enable
      Me("Choise" & x).Picture = Nz(rst![ChoisePicture],"") ' To put a picture in the button - added by me as suggested by theDBguy
      Me("Choise" & x).Caption = Nz(rst![Choise],"") 'Put the choise in the button's caption
      Me("Choise" & x).Tag = rst![RightAnswer] 'Mark the button as right or wrong choise
      rst.MoveNext
    Next x
 

theDBguy

I’m here to help
Staff member
Local time
Today, 10:29
Joined
Oct 29, 2018
Messages
21,473
also one more thing how do i change the size of picture so its just a clip or cant i as its on a command button and not an image box
With regards to displaying a "clipped" image, I am not sure there's anything we can do here. I hope somebody else jumps in with an idea. If not, you may have to make sure you use images with the proper size instead. Cheers!
 

murray83

Games Collector
Local time
Today, 18:29
Joined
Mar 31, 2017
Messages
729
On second thought... You could probably get rid of the If/Then/Else part like this:
Rich (BB code):
    For x = 1 To 4 'Prepare the 4 choises button
      Me("Choise" & x).Visible = True 'Make the button visible
      Me("Choise" & x).Enabled = True 'Make the button enable
      Me("Choise" & x).Picture = Nz(rst![ChoisePicture],"") ' To put a picture in the button - added by me as suggested by theDBguy
      Me("Choise" & x).Caption = Nz(rst![Choise],"") 'Put the choise in the button's caption
      Me("Choise" & x).Tag = rst![RightAnswer] 'Mark the button as right or wrong choise
      rst.MoveNext
    Next x

DBguy, thanks a million works like a charm went for your second option as looks a bit cleaner, and good to know i wasnt far off the actaul answer, if i hadnt been to tired i may of spotted, but cheers again

and please see atatched for anybody wanting to see the same thing, once got it as i want ill upload in to example database section
 

Attachments

  • Quiz-A-Rama - Picture in Command Button Working.accdb
    1.6 MB · Views: 234
Last edited:

Users who are viewing this thread

Top Bottom