borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2012 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Next Topic (run file save command) New Topic New Poll Post Reply
AndreaVB Forum : VB General : vb6 - Edit records
Poster Message
j7-na
Level: Whizz Kid

Registered: 17-11-2011
Posts: 14

icon vb6 - Edit records

Hi boys,
I'm trying to edit a certain record in a access database using vb6. The code I wrote is:


Private Sub Command1_Click()
Dim valore As String
Dim valore1 As String
valore1 = Form1.Text1.Text
valore = Text7.Text  

Set Conn6 = New ADODB.Connection
Set RS6 = New ADODB.Recordset
  
Conn6.Open str
RS6.Open "Table1", Conn6, 1

Conn6.Execute "UPDATE Table1 SET Password=" & valore & _
" WHERE Password =" & RS6("Password")  

RS6.Close
Conn6.Close
End Sub



The program crashes when I click on the Command, but I can not understand the problem. What's wrong?


Thanks!  

[Edited by j7-na on 11-02-2012 at 12:24 PM GMT]

11-02-2012 at 10:32 AM
View Profile Send Email to User Show All Posts | Quote Reply
GeoffS
Level: VB Lord


Registered: 29-09-2004
Posts: 637
icon Re: vb6 - Edit records

There are 2 ways to UPDATE data in a database table using ADODB against an Open Connection :-
Open a Recordset, Find the Row you want to update, Change the value(s) of the data, Run the Update command against the recordset, Close the Recordset.
OR
Open a "Command" Object, set the Command.Text to your Update Statement, Execute the Command, Close the Command Object.
You are mixing the 2 together.
You need to Identify which Data Row in Table1 needs to be updated with the new "Password" - You are trying to match it against the same Field in the Recordset that you have opened, but you have not moved to a record by using "RS6.MoveFirst" so there is a Null Value in RS6("Password") which is one reason why this is not working. A more logical way would be to match the Row to update against a Username or, better still, the RowID.
Let us assume that you have a User whose name is "John" and it is his password we want to update.
Try this against your Open Connection :-
NOTE that the SQL Statement needs the inserted String Variables valore and strUser to be surrounded by a single quote mark ( ' )

Dim cmd As New ADODB.Command
Dim strSQL As String
Dim strUser As String

strUser = "John"
strSQL = "UPDATE Table1 SET Password='" & valore & _
"' WHERE (UserName = '" & strUser  & "')"

cmd.CommandType = adCmdText
cmd.ActiveConnection = Conn6
cmd.CommandText = strSQL
cmd.Execute
Set cmd = Nothing





[Edited by GeoffS on 13-02-2012 at 04:10 PM GMT]

____________________________
multi-tasking - the ability to hang more than one app. at the same time.

13-02-2012 at 03:47 PM
View Profile Send Email to User Show All Posts | Quote Reply
j7-na
Level: Whizz Kid

Registered: 17-11-2011
Posts: 14
icon Re: vb6 - Edit records

I wrote this:

Private Sub Command1_Click()
Dim valore As String
Dim cmd As New ADODB.Command
Dim strSQL As String
Dim strUser As String
Set Conn6 = New ADODB.Connection
Set RS6 = New ADODB.Recordset  
Conn6.Open str6
valore = Text7.Text
strUser = "John"
strSQL = "UPDATE Table1 SET Password='" & valore & _ "' WHERE (Matricola = '" & strUser & "')"  
cmd.CommandType = adCmdText
cmd.ActiveConnection = Conn6
cmd.CommandText = strSQL
cmd.Execute
Set cmd = Nothing    
End Sub


VB6 error on: "Run-time error '-2147217900(80040e14)"

At line:  
cmd.Execute


What should I change?

[Edited by j7-na on 25-02-2012 at 09:43 AM GMT]

25-02-2012 at 09:42 AM
View Profile Send Email to User Show All Posts | Quote Reply
GeoffS
Level: VB Lord


Registered: 29-09-2004
Posts: 637
icon Re: vb6 - Edit records

That's a Syntax Error - which could be my fault as I failed to spot that you had used a Reserved Word for a Fieldname.
Try putting Square Brackets around the Fieldname "Password"
So :-
strSQL = "UPDATE Table1 SET [Password] = '" & valore & "'  WHERE (Matricola = '" & strUser & "')"  
  

____________________________
multi-tasking - the ability to hang more than one app. at the same time.

29-02-2012 at 03:16 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : vb6 - Edit records
Next Topic (run file save command) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2012 Andrea Tincaniborder