Displaying an image in VB6 retrieved from SQL Server 2005
Attachment 91371
I am new to VB6. My shop uses VB6 and SQL Server 2005. What I would like to do is create 2 forms; the first form has a button. When clicked, it will display the second form which displays a jpg image (by setting the Form1.Picture property) retrieved from a table called CapexPhotos. This table only have 2 fields; CCode (bigint) and CapexPhoto1 (varbinary(MAX). I have searched the internet for 3 days without success in finding an answer. Please help. Thank you.
Option Explicit
Private Sub cmdPicture1_Click()
Private Sub Form_Load()
cnn.Close
Set cnn = Nothing
Set cmd = NothingEnd Sub
Private Sub Form_Unload(Cancel As Integer)
Attachment 91371
I am new to VB6. My shop uses VB6 and SQL Server 2005. What I would like to do is create 2 forms; the first form has a button. When clicked, it will display the second form which displays a jpg image (by setting the Form1.Picture property) retrieved from a table called CapexPhotos. This table only have 2 fields; CCode (bigint) and CapexPhoto1 (varbinary(MAX). I have searched the internet for 3 days without success in finding an answer. Please help. Thank you.
Option Explicit
Private Sub cmdPicture1_Click()
If cmdPicture1.Caption = "Show Picture 1" Then
cmdPicture1.Caption = "Hide Picture 1"
Load Form2
Form2.Height = Form2.Picture.Height * 0.6
Form2.Width = Form2.Picture.Width * 0.57
Form2.Top = (Screen.Height / 2) - (Form2.Height / 2)
Form2.Left = (Screen.Width / 2) - (Form2.Width / 2)
Form2.Show
Else
cmdPicture1.Caption = "Show Picture 1"
Form2.Height = Form2.Picture.Height * 0.6
Form2.Width = Form2.Picture.Width * 0.57
Form2.Top = (Screen.Height / 2) - (Form2.Height / 2)
Form2.Left = (Screen.Width / 2) - (Form2.Width / 2)
Form2.Show
Else
cmdPicture1.Caption = "Show Picture 1"
Form2.Hide
Unload Form2
End If
End SubPrivate Sub Form_Load()
Dim xCCode As Integer
Dim rst As ADODB.Recordset
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
cnn.Open "Provider=SQLOLEDB.1;Password=Password123;Persist Security Info=True;User ID=SQLServerDB;Initial Catalog=MyCatalog;Data Source=MySource"
xCCode = 8520
If rst Is Nothing Then Set rst = New ADODB.Recordset
With rst
Dim rst As ADODB.Recordset
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
cnn.Open "Provider=SQLOLEDB.1;Password=Password123;Persist Security Info=True;User ID=SQLServerDB;Initial Catalog=MyCatalog;Data Source=MySource"
xCCode = 8520
If rst Is Nothing Then Set rst = New ADODB.Recordset
With rst
'*** Picture #1
If .State = adStateOpen Then .Close
.Open "SELECT COUNT(CapexPhoto1) FROM CapexPhotos WHERE CCode='" & xCCode & "'", cnn, adOpenStatic
If .Fields(0).Value = 0 Then
If .State = adStateOpen Then .Close
.Open "SELECT COUNT(CapexPhoto1) FROM CapexPhotos WHERE CCode='" & xCCode & "'", cnn, adOpenStatic
If .Fields(0).Value = 0 Then
cmdPicture1.Enabled = False
Else
*** This is where I need help.***
'Retrieve Picture #1 from the database using the CCode
'Set the Picture property of Form2 to Picture #1
cmdPicture1.Enabled = True
'Retrieve Picture #1 from the database using the CCode
'Set the Picture property of Form2 to Picture #1
cmdPicture1.Enabled = True
End If
.Close
End With
cnn.Close
Set cnn = Nothing
Set cmd = Nothing
Private Sub Form_Unload(Cancel As Integer)
If Form2.Visible = True Then
Form2.Hide
Unload Form2
End If
End Sub