Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grid From 1

PrimoSteve

Technical User
Aug 11, 2021
25
0
1
IE
Hello All,

I have a subform that acts like a grid that displays 24 command button ( 4 Rows and 6 columns)

tblCmds
cmdID Short Text
cmdCaption Short Text
cmdPhotoUrl Short Text


Private Sub Form_Load()
On Error GoTo FormLoadErr:

Dim ctl As Control
Me.cTemp.SetFocus


For Each ctl In Me.Controls

If TypeOf ctl Is CommandButton Then

If ctl.Name Like "cmdOrder*" Then

ctl.Visible = True
ctl.Caption = DLookup("[cmdCaption]", "tblCmds", "[cmdID]='" & ctl.Name & "'")
If ctl.Caption = "Empty" Then ctl.Visible = False

End If

End If

Next ctl

DoCmd.Save

Exit Sub

FormLoadErr:
MsgBox ctl.Name & "-" & err.Description

End Sub


The following code above works fine to display the caption but when I add the following line of code to display the picture.

ctl.Picture = DLookup("[cmdPhotoUrl]", "tblCmds", "[cmdID]='" & ctl.Name & "'")

I get this error

"cmdOrder02-SampleDB can't open tje file
'C:\Users\Admin\Documents\Parts1.png'."

Any assistance would help in writing this code the correct way



For Each ctl In Me.Controls

If TypeOf ctl Is CommandButton Then

If ctl.Name Like "cmdOrder*" Then

ctl.Visible = True
ctl.Caption = DLookup("[cmdCaption]", "tblCmds", "[cmdID]='" & ctl.Name & "'")
ctl.Picture = DLookup("[cmdPhotoUrl]", "tblCmds", "[cmdID]='" & ctl.Name & "'")
If ctl.Caption = "Empty" Then ctl.Visible = False

End If

End If

Next ctl

DoCmd.Save


Regards
Primo
 
Command buttons only directly support bmp and ico file formats, I am afraid. Although a workaround is to use an image control as a proxie, and then transfer the picture from the proxie to the command button via the PictureData property.
 
Command buttons only directly support bmp and ico file formats, I am afraid. Although a workaround is to use an image control as a proxie, and then transfer the picture from the proxie to the command button via the PictureData property.
I've tried bmp file format but still to know avail

Primo

Command buttons only directly support bmp and ico file formats, I am afraid. Although a workaround is to use an image control as a proxie, and then transfer the picture from the proxie to the command button via the PictureData property.

Command buttons only directly support bmp and ico file formats, I am afraid. Although a workaround is to use an image control as a proxie, and then transfer the picture from the proxie to the command button via the PictureData property.
 
I've tried using the BMP file format but still no avail

Regards
 
Ive attached the file for viewing hope this might help
 

Attachments

  • GridDBA.zip
    51.3 KB · Views: 5
Have you attempted to troubleshoot by writing similar code that sets the picture of a single command button with hard coded values? Isolating bit by bit is a great troubleshooting method. Don't add any complexity until you have success with the previous step.
 
Hi Duane, Ive been doing that exactly. I've managed to the pictures to show if the the link field for the photo is not null. But if the field is empty and null I get the error for the command button with the empty field e.g. cmdOrder2 which has no data

"cmdOrder02-SampleDB can't open tje file
'C:\Users\Admin\Documents\Parts1.png'."
 
With:
ctl.Picture = DLookup(...
what do you get from your DLookup? Is it just the name of the file, like MyPicture.bmp? Or full path with file name, like C:\SomeFolder\Images\MyPicture.bmp?
 
So, quick look at this - and DLOOKUP actually does the hard work of converting the underlying file to a supported type, so that is not the problem

So, have to ask, you are trying to open files in different user profiles. Does the account running the program have the correct access rights to do this?
 
Its the full path C:\SomeFolder\Images\MyPicture.bmp that's on the table field cmdPhotoUrl

tblCmds
cmdID Short Text
cmdCaption Short Text
cmdPhotoUrl Short Text
 
So, quick look at this - and DLOOKUP actually does the hard work of converting the underlying file to a supported type, so that is not the problem

So, have to ask, you are trying to open files in different user profiles. Does the account running the program have the correct access rights to do this?

Yes, the file can be open by different users but it shouldn't make a difference. I have attached the file so you can view it yourself
 

Attachments

  • GridDBA.zip
    51.3 KB · Views: 2
I'm not sure what you want to achieve, but hope this helps.
I copied your 'butter.jpg' to my c:\ and renamed it to 'butter.bmp'
In tblCmds i changed the first 7 PhotoURL's to C:\butter.bmp
In the form load code I added Dim pic as variant
I modified the code as in the attached screensnip and got the result in the other screensnip. Hope this helps in some way.
 

Attachments

  • Screenshot 2024-08-22 003728.png
    Screenshot 2024-08-22 003728.png
    15.1 KB · Views: 8
  • Screenshot 2024-08-22 003827.png
    Screenshot 2024-08-22 003827.png
    65.9 KB · Views: 7
I'm not sure what you want to achieve, but hope this helps.
I copied your 'butter.jpg' to my c:\ and renamed it to 'butter.bmp'
In tblCmds i changed the first 7 PhotoURL's to C:\butter.bmp
In the form load code I added Dim pic as variant
I modified the code as in the attached screensnip and got the result in the other screensnip. Hope this helps in some way.
 
@ OzFoxy - This worked perfect many thanks for your help and everyone's input, You can see attachment and idea of what I/m trying to achieve, still more work to do
 

Attachments

  • PO GRID.jpg
    PO GRID.jpg
    142.8 KB · Views: 5
Yes, the file can be open by different users but it shouldn't make a difference. I have attached the file so you can view it yourself
Primo, I'd already downloaded your example and tested it. it worked fine here (when I populated the table with files that actually existed on my PC; it's how I determined that dlookup was doing some interesting work in the background.

The only way I could get replicate your symptoms problem was if I changed the permissions,
 
Hi Strongman, I'll have look it's possible I haven't tested on another pc or network.

But I also have another issue which I think has to do with a Null field

I'm getting a "cmdOrder01-Type mismatch" error if the PhotoURL field is empty or Null
Any idea how do I code this to ignore the data field if it's empty/null or missing the linked photo address data

Private Sub Form_Load()
On Error GoTo FormLoadErr:

Dim ctl As Control
Dim pic As Variant
Me.cTemp.SetFocus



For Each ctl In Me.Controls
X = 1

If TypeOf ctl Is CommandButton Then

If ctl.Name Like "cmdOrder*" Then

ctl.Visible = True
ctl.Caption = DLookup("[cmdCaption]", "tblCmds", "[cmdID]='" & ctl.Name & "'")
pic = DLookup("[PhotoURL]", "tblCmds", "[cmdID]='" & ctl.Name & "'")
ctl.Picture = pic
If ctl.Caption = "Empty" Then ctl.Visible = False

X = X + 1
End If

End If

Next ctl

DoCmd.Save



Exit Sub

FormLoadErr:
MsgBox ctl.Name & "-" & err.Description

End Sub
 
One way would be (and that would me my preferred way) to not allow NULL or empty PhotoURL field in tblCmds table.

Other way:
Code:
pic = DLookup("[PhotoURL]", "tblCmds", "[cmdID]='" & ctl.Name & "'")
If IsNull(pic) or pic & "" = "" Then
    'Do something
Else
    ctl.Picture = pic
End If
 
Hi Andrzejek,

Thanks! I've actually tried your way but I'm getting a compile error: Next without For

Private Sub Form_Load()
On Error GoTo FormLoadErr:

Dim ctl As Control
Me.cTemp.SetFocus
Dim pic As Variant


For Each ctl In Me.Controls


If TypeOf ctl Is CommandButton Then

If ctl.Name Like "cmdOrder*" Then

ctl.Visible = True
ctl.Caption = DLookup("[cmdCaption]", "tblCmds", "[cmdID]='" & ctl.Name & "'")
pic = DLookup("[PhotoURL]", "tblCmds", "[cmdID]='" & ctl.Name & "'")
If IsNull(pic) Or pic & "" = "" Then
MsgBox " No Image Available"
Else
ctl.Picture = pic
If ctl.Caption = "Empty" Then ctl.Visible = False


End If

End If

Next ctl

DoCmd.Save

Exit Sub

FormLoadErr:
MsgBox ctl.Name & "-" & err.Description

End Sub
 
With indents you can easily find closing End If missing:
Code:
For Each ctl In Me.Controls
    If TypeOf ctl Is CommandButton Then
        If ctl.Name Like "cmdOrder*" Then
            ctl.Visible = True
            ctl.Caption = DLookup("[cmdCaption]", "tblCmds", "[cmdID]='" & ctl.Name & "'")
            pic = DLookup("[PhotoURL]", "tblCmds", "[cmdID]='" & ctl.Name & "'")
            If IsNull(pic) Or pic & "" = "" Then
                MsgBox " No Image Available"
            Else
                ctl.Picture = pic
                If ctl.Caption = "Empty" Then ctl.Visible = False
            End If
        End If
    ' ?????
Next ctl
 

Part and Inventory Search

Sponsor

Back
Top