Connecting Tech Pros Worldwide Forums | Help | Site Map

Problems with update sql statemnt in vb6

Newbie
 
Join Date: Sep 2008
Posts: 11
#1: Sep 30 '08
I have a table called SupplierInfor with tables:
SupplierID - Autonumber
Date - date
SupplierName - Text
SupplierAddress - text
Telephone - Number
email - text
Town - text
country - text
I want to update all records in this table where supplierID is a certain number. The code i have is as below:
Expand|Select|Wrap|Line Numbers
  1. dim strSQL as string
  2. strSQL = "UPDATE SupplierInfor SET " & _
  3.     "[Date]='" & txtDate.Text & "', " & _
  4.     "[SupplierName]='" & txtSuppName.Text & "', " & _
  5.     "[SupplierAddress]='" & txtSuppAddress.Text & "', " & _
  6.     "[Telephone]='" & txtTelephone.Text & "', " & _
  7.     "[email]='" & txtEmail.Text & "', " & _
  8.     "[Town]=" & cboTown.Text & ", " & _
  9.     "[Country]=" & cboCountry.Text & " WHERE [SupplierID]='" & Val(txtSuppID.Text) & "'"
  10.  
  11.    Cn.Execute strSQL
When i run the code i get the error message "no value given for one or more required parameters" Am using vb6 and ADO connection to MS Access 2000.
What is the problem with my code? PLease help me and i will appreciate any assistance.

QVeen72's Avatar
Moderator
 
Join Date: Oct 2006
Location: Bangalore
Posts: 1,385
#2: Sep 30 '08

re: Problems with update sql statemnt in vb6


Hi,

Dates need to be enclosed with a Hash:
And since you say Supplier ID is Autonumber and Telephone is Numeric, no need to Wrap it with Single quotes:

Expand|Select|Wrap|Line Numbers
  1. dim strSQL as string
  2. strSQL = "UPDATE SupplierInfor SET " & _
  3.     "[Date]=#" & txtDate.Text & "#,  " & _
  4.     "[SupplierName]='" & txtSuppName.Text & "', " & _
  5.     "[SupplierAddress]='" & txtSuppAddress.Text & "', " & _
  6.     "[Telephone]=" & Val(txtTelephone.Text) & ", " & _
  7.     "[email]='" & txtEmail.Text & "', " & _
  8.     "[Town]='" & cboTown.Text & "', " & _
  9.     "[Country]='" & cboCountry.Text & "' WHERE [SupplierID]=" & Val(txtSuppID.Text) 
  10.  
Regards
Veena
Newbie
 
Join Date: Sep 2008
Posts: 11
#3: Oct 1 '08

re: Problems with update sql statemnt in vb6


Thanks alot Veena
It worked and now am so grateful for that. But their is another problem, when i try to delete a record in the same table using the code below, it runs without any error but does not delete any record even if i keep on clicking the delete button. Please look at the code and help me with the area of the problem.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDelete_Click()
  2. Dim kuiz As String
  3. kuiz = MsgBox("Are you sure you want to DELETE the current record?", vbYesNo + vbQuestion, "Confirm delete...")
  4. If kuiz = vbYes Then
  5. R3.Open "SELECT * FROM SupplierInfor where SupplierName='" & Trim(txtSuppName.Text) & "'", Cn, adOpenDynamic, adLockBatchOptimistic
  6.     If Not R3.EOF Then
  7.         R3.Delete
  8.     End If
  9. R3.Close
  10. Set R3 = Nothing
  11. End If
  12. End Sub
Thanks again for your help.
QVeen72's Avatar
Moderator
 
Join Date: Oct 2006
Location: Bangalore
Posts: 1,385
#4: Oct 1 '08

re: Problems with update sql statemnt in vb6


Hi,

No Need TO Open a Record and Delete.. Just Run a Delete Query:

Expand|Select|Wrap|Line Numbers
  1. Cn.Execute "Delete FROM SupplierInfor where SupplierName='" & Trim(txtSuppName.Text) & "'"
  2.  
Regards
Veena
Newbie
 
Join Date: Sep 2008
Posts: 11
#5: Oct 1 '08

re: Problems with update sql statemnt in vb6


Hi Veena,
Thanks for this help, just imagine the delete code just works fine, Thanks coz i was stuck in this. I just don't seem to get someting clear about the UPDATE SQL. Am trying to update a table called ITEM_MASTER_TB WITH THE FOLLOWING FIELDS:
ID - Autonumber
Bardcode - text
Description - text
Category - text
Units - text
Priceperunit - Currency
Quantity - Number
Totalcost - Curency
SupplierName - text
DateOfSupply - date
VAtPer - currency

the code for updating the table is as follows, please forgive me for asking too many questions its only that am not good in SQL.

Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2.    strSQL = "UPDATE ITEM_MASTER_TBL SET " & _
  3.     "[DESCRIPTION]='" & txtItemDescription.Text & "', " & _
  4.     "[CATEGORY]='" & cboCategory.Text & "', " & _
  5.     "[UNITS]='" & cboUnits.Text & "', " & _
  6.     "[PRICEPERUNIT]='" & txtPricePerUnit.Text & "', " & _
  7.     "[QUANTITY]=" & txtQuantity.Text & ", " & _
  8.     "[TOTALCOST]='" & txtCost.Text & "', " & _
  9.     "[DATEOFSUPPLY]='" & txtDate.Text & "', " & _
  10.     "[VATPER]='" & txtVATPER.Text & "' WHERE [BARCODE]=" & txtBarcode.Text & ""
  11.    Cn.Execute strSQL
the code returns 'DATA TYPE MISMATCH IN CRITERIA EXPRESSION'. Just maybe it is the single quotes have put in wrong place.
Newbie
 
Join Date: Sep 2008
Posts: 11
#6: Oct 1 '08

re: Problems with update sql statemnt in vb6


Hi Veena,
Sory I just got the problem, its my ignorance the same problem again. I forgot HASH for date column and single quote for txtBarcode.text. I have done the corrections and its OK. Thanks Bro.
Newbie
 
Join Date: Sep 2008
Posts: 11
#7: Oct 1 '08

re: Problems with update sql statemnt in vb6


Hi Veena,
This i know is the logic maybe. In the same project am working on, i want to insert several records in a table using the INSERT INTO SQL. The records i want to insert are in a MSflexgrid that i have populated with data. My coden to do this is as below:
Expand|Select|Wrap|Line Numbers
  1. Dim q As Integer
  2.     With Me.MSFlexPOS
  3.     For q = 1 To (.Rows - 1)
  4.  
  5.         Cn.Execute "INSERT INTO POS_TBL(INVNO,INVDATE,BARCODE,DESCRIPTION,CATEGORY,UNITS,COST,QTY,VATPER,STOTAL,VATAMT,LINETOTAL)VALUES(" & _
  6.             Me.TXTINVNUMBER.Caption & ",#" & _
  7.             Me.TXTINVDATE.Caption & "#,'" & _
  8.                 .TextMatrix(q, 1) & "','" & _
  9.                 .TextMatrix(q, 2) & "','" & _
  10.                 .TextMatrix(q, 3) & "','" & _
  11.                 .TextMatrix(q, 4) & "'," & _
  12.                 .TextMatrix(q, 5) & "," & _
  13.                 .TextMatrix(q, 6) & "," & _
  14.                 .TextMatrix(q, 7) & "," & _
  15.             Val(.TextMatrix(q, 8)) & "," & _
  16.             Val(.TextMatrix(q, 9)) & "," & _
  17.             Val(.TextMatrix(q, 10)) & ")"
  18.         Next q
  19.     End With
The problem is that it can't loop through the records while inserting them, but if i do away with the for statement, it works but inserts only the first record in the msflexgrid. Do you have an idea how to insert several records while looping in a table. Your help is invaluable.
QVeen72's Avatar
Moderator
 
Join Date: Oct 2006
Location: Bangalore
Posts: 1,385
#8: Oct 2 '08

re: Problems with update sql statemnt in vb6


Hi,

Ms.Veena here,
Query Seems to be OKay.. Let me know what is your Database and What is the Provider in Connection String.. Is there any PrimaryKey for the Table..?
If you have a PK and If you are trying to Insert mutiple Records for same PK, then records cannot be Inserted..
If there is no PK, then Try using Begin and Commit Trans,,
Some thing like this :

Expand|Select|Wrap|Line Numbers
  1. Cn.BeginTrans
  2. Cn.Execute "<MySQL Statement>"
  3. Cn.CommitTrans
  4.  
REgards
Veena
Newbie
 
Join Date: Sep 2008
Posts: 11
#9: Oct 2 '08

re: Problems with update sql statemnt in vb6


Thanks Ms Veena,
My Database is MS access 2000 called POS_DB and the connection string is:
Cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Point-Of-Sale\POS_DB.MDB; Persist Security Info=False"
Cn.CursorLocation = adUseClient
The table am inserting multiple records has a primary key i.e. ID - Autonumber
Thanks in advance.
QVeen72's Avatar
Moderator
 
Join Date: Oct 2006
Location: Bangalore
Posts: 1,385
#10: Oct 2 '08

re: Problems with update sql statemnt in vb6


Hi,

Did you try using Begin and committrans...?
Also Try to Keep a BreakPoint at Cn.Execute and immediately after executing check if the recs are inserted in the Accessdb.. Check after every insert..
It takes a Few Seconds in Access To Get re-freshed..

But I guess, You can Open a Recset To add data..
Thats what everyone normally follow..
To Delete /Edit Records use "Execute <SQL>"
To Add Records Open a RecordSet....


Regards
Veena
Newbie
 
Join Date: Sep 2008
Posts: 11
#11: Oct 2 '08

re: Problems with update sql statemnt in vb6


Hi,

Sorry, i didn't try coz i have never used Begin or Committrans in vb6. Actually i do not know where to code it maybe you could sort me out on this.Another clarification from you please, When i want to use the Recordset instead of the Execute SQL, is it a must i have a ADO control on the form for it to work? Do you have an example of using the Recordset? Much Gratitude
QVeen72's Avatar
Moderator
 
Join Date: Oct 2006
Location: Bangalore
Posts: 1,385
#12: Oct 3 '08

re: Problems with update sql statemnt in vb6


Hi,

Not Necessary to have DATA control to open recordset...
You can use ADO Objects...

Some thing Like This :
assuming you connection is Open..
Expand|Select|Wrap|Line Numbers
  1. Dim RST As New ADODB.Recordset
  2. RST.Open "Select * From MyTable",Cn,adOpenDynamic, adLockOptimistic
  3. RST.AddNew
  4. RST("MyCol1") = Text1.Text
  5. RST("MyCol2") = Text2.Text
  6. RST("MyCol3") = Text3.Text
  7. RST.Update
  8. RST.Close
  9.  
You Can use the Same Code To Loop through FlexGrid Rows..(Just your TextMatrix Row will change)

Regards
Veena
Newbie
 
Join Date: Sep 2008
Posts: 11
#13: Oct 3 '08

re: Problems with update sql statemnt in vb6


Let me give it a try and i hope this sorts me out. Thanks alot for your invaluable assistance you've provided. You remain in my memory. Incase am stuck, i'll seek your help. Nice time.
Newbie
 
Join Date: Sep 2008
Posts: 11
#14: Oct 8 '08

re: Problems with update sql statemnt in vb6


Am sorry i was not able to work through your suggestion. I wanted to insert several records into a table while looping throuth the records in a msflexgrid with the code below:


1. Dim q As Integer
2. With Me.MSFlexPOS
3. For q = 1 To (.Rows - 1)
4.
5. Cn.Execute "INSERT INTO POS_TBL(INVNO,INVDATE,BARCODE,DESCRIPTION,CATEGORY ,UNITS,COST,QTY,VATPER,STOTAL,VATAMT,LINETOTAL)VAL UES(" & _
6. Me.TXTINVNUMBER.Caption & ",#" & _
7. Me.TXTINVDATE.Caption & "#,'" & _
8. .TextMatrix(q, 1) & "','" & _
9. .TextMatrix(q, 2) & "','" & _
10. .TextMatrix(q, 3) & "','" & _
11. .TextMatrix(q, 4) & "'," & _
12. .TextMatrix(q, 5) & "," & _
13. .TextMatrix(q, 6) & "," & _
14. .TextMatrix(q, 7) & "," & _
15. Val(.TextMatrix(q, 8)) & "," & _
16. Val(.TextMatrix(q, 9)) & "," & _
17. Val(.TextMatrix(q, 10)) & ")"
18. Next q
19. End With

but the program generates a syntax error in the insert into statement. The table has a primaly key which is autonumber. Please sort me out coz am seriously stuck. Consider an elaborate answer please.
Thanks in advance
Reply