|
-
May 20th, 2011, 04:13 AM
#1
Thread Starter
Addicted Member
i get Bad variable type error when saving record
Hi guys
I have a problem in saving an image to database.
I'm using MySQL database then name of the field is "Pic" and the data type is LongBlob.
And my problem was i get an error when i don't upload an image when saving the record. then it appears an error Bad Variable type.
if i upload an image it works fine and the record is save there is no problem in saving.
Code:
Option Explicit
Dim bytData() As Byte ' Declarations
Private Sub Command3_Click()
On Error Resume Next
CommonDialog1.Filter = "All Files (*.*)|*.*|GIF Files (*.gif)|*.gif|JPEG Files (*.jpg)|*.jpg|Bitmap Files (*.bmp)|*.bmp"
CommonDialog1.ShowOpen
CommonDialog1.DialogTitle = "Select Picture" 'sets the title of it.
txtPicPath.Text = CommonDialog1.FileName
Open CommonDialog1.FileName For Binary As #1
ReDim bytData(FileLen(CommonDialog1.FileName))
Get #1, , bytData
Close #1
End Sub
Private Sub AddFaculty()
On Error GoTo ErrorDito
Dim strSQL As String
Dim adoCommand As ADODB.Command
Dim FacultyString As String
Set adoCommand = New ADODB.Command
FacultyString = "Faculty"
Debug.Print bytData
strSQL = "INSERT INTO tblFaculty (FacultyNumber,LastName,FirstName,MiddleName,Gender,UserType,Pass,Pic) VALUES(?,?,?,?,?,?,?,?)"
With adoCommand
.ActiveConnection = cn
.CommandType = adCmdText
.CommandText = strSQL
.Prepared = True
.Parameters.Append .CreateParameter(, adVarChar, adParamInput, 20, Text1.Text)
.Parameters.Append .CreateParameter(, adVarChar, adParamInput, 50, Text2.Text)
.Parameters.Append .CreateParameter(, adVarChar, adParamInput, 50, Text3.Text)
.Parameters.Append .CreateParameter(, adVarChar, adParamInput, 50, Text4.Text)
.Parameters.Append .CreateParameter(, adVarChar, adParamInput, 10, Combo1.Text)
.Parameters.Append .CreateParameter(, adVarChar, adParamInput, 10, FacultyString)
.Parameters.Append .CreateParameter(, adVarChar, adParamInput, 50, Text5.Text)
.Parameters.Append .CreateParameter(, adVarBinary, adParamInput, 65535, bytData)
.Execute , , adCmdText + adExecuteNoRecords 'the last two arguments makes the execution of the command faster
End With
Set adoCommand = Nothing
MsgBox "New record added", vbInformation
'requery the list
frmUserMenu.showlist
frmUserMenu.CountRecord
Unload Me
Exit Sub
ErrorDito:
MsgBox Err.Description, vbCritical
End Sub
The highlighted point the error
-
May 20th, 2011, 05:00 AM
#2
Re: i get Bad variable type error when saving record
If you don't have data to put in it, don't assign a value to that Parameter, ie:
Code:
.Parameters.Append .CreateParameter(, adVarBinary, adParamInput, 65535)
-
May 20th, 2011, 06:32 AM
#3
Thread Starter
Addicted Member
Re: i get Bad variable type error when saving record
If bytData = 0 Then
.Parameters.Append .CreateParameter(, adVarBinary, adParamInput, 999999)
Else
.Parameters.Append .CreateParameter(, adVarBinary, adParamInput, 999999, bytData)
End If
i do this and i got an error type mismatch then highlighted the "="
-
May 20th, 2011, 06:41 AM
#4
Re: i get Bad variable type error when saving record
That is entirely expected, because bytData is an array, so you can't check it in the same way as you would a simple numeric variable.
The simplest way would be to have a Boolean variable to say whether or not you have assigned a value to the array yet.
Note also that your use of On Error Resume Next is very bad, and is likely cause various problems. For more information, see the article What is wrong with using "On Error Resume Next"? from our Classic VB FAQs (in the FAQ forum)
-
May 20th, 2011, 06:56 AM
#5
Thread Starter
Addicted Member
Re: i get Bad variable type error when saving record
If boolean i will use, how can i code that? im not so good in coding
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|