| | |
Connectivity of SQL 2000 + VB using ODBC
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
![]() |
how do you expect this code to run? the first statement contains a big error. apart from this, the insert statement which you have written & passed to the variable query contains syntax errors. errors due to nontermination of string constant and invalid sql command. the following is the corrected syntax. try this...
regards
Shouvik
Dim gcn As New ADODB.Connection Dim query As String gcn.ConnectionString="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=sonia" gcn.Open Msgbox "An active connection has been established." query="insert into stuinfo(roll,name,marks) values('" & txtroll.text & "','" & txtname.text & "'," & val(txtmarks.text) & ")" gcn.Execute (query) MsgBox "Record Inserted" gcn.Close ''your insert statement should look like this :- INSERT INTO STUINFO(ROLL,NAME,MARKS) VALUES('1','SONIA',100)
regards
Shouvik
Last edited by choudhuryshouvi; Apr 15th, 2008 at 1:34 am.
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Have a problem? Don't worry just give me a call and I'll fix it for you.
•
•
Join Date: Mar 2008
Posts: 324
Reputation:
Solved Threads: 7
Hi Shouvik ,
query="insert into stuinfo(roll,name,marks) values('" & txtroll.text & "','" & txtname.text & "'," & val(txtmarks.text) & ")"
1)If we broke the above line into two lines y the error comes. To split the above line what I do??
2)Second thing when i run the form,Msg comes Record inserted and when i click on ok then the form comes. And when we fill the fields & click on OK button,then the msg do not come.
3) Third thing,roll no is integer na then y u use single quotes as below-
'" & txtroll.text & "'
4)Fourth thing,Record is inserted as below-
roll name marks
0 blank 0
0 blank 0
5)This method of connectiong to the database is ADO.rite?
query="insert into stuinfo(roll,name,marks) values('" & txtroll.text & "','" & txtname.text & "'," & val(txtmarks.text) & ")"
1)If we broke the above line into two lines y the error comes. To split the above line what I do??
2)Second thing when i run the form,Msg comes Record inserted and when i click on ok then the form comes. And when we fill the fields & click on OK button,then the msg do not come.
3) Third thing,roll no is integer na then y u use single quotes as below-
'" & txtroll.text & "'
4)Fourth thing,Record is inserted as below-
roll name marks
0 blank 0
0 blank 0
5)This method of connectiong to the database is ADO.rite?
Last edited by sonia sardana; Apr 15th, 2008 at 7:17 am.
ok......sonia......see your answers inline.
to break the line use this syntax :-
see the Bolded part. it is the syntax (an ampersand followed by a space and an underscore) for breaking the line. this process is called Line Continuation.
sounds confusing!!! would you mind in explaining this a little bit clear?
i was just assumed that. there is no hard-n-fast rule that roll no becomes always integer. it can be alphanumeric like 'R001'. so to show you an example i used that one. you have to change it accordingly based on your data type. if its an integer avoid that single quotes otherwise use those.
well I am unable to speak a single word without see what have you done so far. because the statement i used here is absolutely correct. now if its not so problem for you would you mind in posting your code for inserting record here?
yes, you are 1000% correct.
regards
Shouvik
•
•
•
•
query="insert into stuinfo(roll,name,marks) values('" & txtroll.text & "','" & txtname.text & "'," & val(txtmarks.text) & ")"
1)If we broke the above line into two lines y the error comes. To split the above line what I do??
query = "insert into stuinfo(roll,name,marks)" & _ "values('" & txtroll.Text & "','" & _ txtname.Text & "'," & Val(txtmarks.Text) & ")"
•
•
•
•
2)Second thing when i run the form,Msg comes Record inserted and when i click on ok then the form comes. And when we fill the fields & click on OK button,then the msg do not come.
•
•
•
•
3) Third thing,roll no is integer na then y u use single quotes as below-
'" & txtroll.text & "'
•
•
•
•
4)Fourth thing,Record is inserted as below-
roll name marks
0 blank 0
0 blank 0
•
•
•
•
5)This method of connectiong to the database is ADO.rite?
regards
Shouvik
Last edited by choudhuryshouvi; Apr 16th, 2008 at 2:01 pm.
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Have a problem? Don't worry just give me a call and I'll fix it for you.
•
•
Join Date: Mar 2008
Posts: 324
Reputation:
Solved Threads: 7
Hey Shouvik,I m trying to insert the records into the DATABASE Table.
Form Designing-Three textboxes , & one Insert button.
I m inserting just numbers in the column roll.
DATABASE TABLE-create table stuinfo(roll int,name varchar(10),marks int)
select * from stuinfo
I m writing the below mentioned coding-
How Can I explain U-
When i run the form,Msg comes Record inserted and when i click on ok then the form comes. And when we fill the fields & click on OK button,then the msg do not come.
U plz Run this coding by urself,then u know what I m trying to say.
Private Sub Form_Load()
Dim gcn As New ADODB.Connection
Dim query As String
gcn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=sonia"
gcn.Open
query = "insert into stuinfo(roll,name,marks) values(" & Val(txtroll.Text) & ",'" & txtname.Text & "'," & Val(txtmarks.Text) & ")"
gcn.Execute (query)
MsgBox "Record Inserted"
gcn.Close
End Sub
roll name marks
0 blank 0
0 blank 0
By saying this i m trying to say that,The records that are inserted into the database are
Always 0 is inserted for roll & marks,
& Blank is inserted for name.
I hope U Undersyand What I m trying to say.
hey one more thing tell me that Mastering in Visual basic 6.0 is good for freshers nor not.
It is not too highfy book na, dat freshers like me can’t understand. Cz I m planning to purchase dat book.
Form Designing-Three textboxes , & one Insert button.
I m inserting just numbers in the column roll.
DATABASE TABLE-create table stuinfo(roll int,name varchar(10),marks int)
select * from stuinfo
I m writing the below mentioned coding-
How Can I explain U-
When i run the form,Msg comes Record inserted and when i click on ok then the form comes. And when we fill the fields & click on OK button,then the msg do not come.
U plz Run this coding by urself,then u know what I m trying to say.
Private Sub Form_Load()
Dim gcn As New ADODB.Connection
Dim query As String
gcn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=sonia"
gcn.Open
query = "insert into stuinfo(roll,name,marks) values(" & Val(txtroll.Text) & ",'" & txtname.Text & "'," & Val(txtmarks.Text) & ")"
gcn.Execute (query)
MsgBox "Record Inserted"
gcn.Close
End Sub
roll name marks
0 blank 0
0 blank 0
By saying this i m trying to say that,The records that are inserted into the database are
Always 0 is inserted for roll & marks,
& Blank is inserted for name.
I hope U Undersyand What I m trying to say.
hey one more thing tell me that Mastering in Visual basic 6.0 is good for freshers nor not.
It is not too highfy book na, dat freshers like me can’t understand. Cz I m planning to purchase dat book.
Last edited by sonia sardana; Apr 16th, 2008 at 4:58 pm.
ok......this is the reason for what happening to you...
first, you have placed the record insertion code in wrong section. you are firing that code in form_load event. in this event vb actually initializes all controls. so at this time your three textboxes contain null values like txtroll="" , txtname="" and txtmarks="" . now when your program fires the statement, it finds nothing in the text boxes. now as per your code it is converting value of those text boxes like ,
val(txtroll.text) --> 0
txtname.text --> null
val(txtmarks.text) --> 0
and storing them in your table.
as you already known that val converts equivalent string to numbers. so when it finds a null value it passes only 0. now all your statements are executing fine coz there is no syntax or compilation errors. so the msgbox "Record Inserted" is being displayed. your code has some logical mistakes.
now you said .
this might has the following causes :-
1. you didn't mention the insertion code under the button.
2. though you have coded it but the msgbox statement is not there.
you have to code your record insertion statements in both of your form_load (sounds crazy but as you told) and button_click events so that the same action can be performed by your program when you wish to do so. now you have written in the code in form_load event. that's why the msgbox is coming when start debugging your program and not coming when you click on the button because clicking on the button generates a new separate event which has no relation with the form_load event--->means the form will not be loaded once again after clicking on the button.
hope you will be understood.
now for your consideration i have written the following code using the same database,provider,table,fields and form design. also there is a screenshot for you.
check this out and inform me what have you got...
regards
Shouvik
first, you have placed the record insertion code in wrong section. you are firing that code in form_load event. in this event vb actually initializes all controls. so at this time your three textboxes contain null values like txtroll="" , txtname="" and txtmarks="" . now when your program fires the
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
gcn.execute "insert..."
val(txtroll.text) --> 0
txtname.text --> null
val(txtmarks.text) --> 0
and storing them in your table.
as you already known that val converts equivalent string to numbers. so when it finds a null value it passes only 0. now all your statements are executing fine coz there is no syntax or compilation errors. so the msgbox "Record Inserted" is being displayed. your code has some logical mistakes.
now you said
•
•
•
•
when click on the insert button the msgbox is not coming
this might has the following causes :-
1. you didn't mention the insertion code under the button.
2. though you have coded it but the msgbox statement is not there.
you have to code your record insertion statements in both of your form_load (sounds crazy but as you told) and button_click events so that the same action can be performed by your program when you wish to do so. now you have written in the code in form_load event. that's why the msgbox is coming when start debugging your program and not coming when you click on the button because clicking on the button generates a new separate event which has no relation with the form_load event--->means the form will not be loaded once again after clicking on the button.
hope you will be understood.
now for your consideration i have written the following code using the same database,provider,table,fields and form design. also there is a screenshot for you.
check this out and inform me what have you got...
general declaration section. global variable Dim gCn As New ADODB.Connection 'will insert a record into your table Private Sub cmdinsert_Click() If Trim(txtroll.Text) = "" Then MsgBox "Please input rollno." txtroll.SetFocus Exit Sub End If If Trim(txtname.Text) = "" Then MsgBox "Please input name." txtname.SetFocus Exit Sub End If If Trim(txtmarks.Text) = "" Then MsgBox "Please input marks." txtmarks.SetFocus Exit Sub End If Dim confirm As Integer confirm = MsgBox("Sure to add this record?", vbYesNo) If confirm = vbNo Then txtroll.SetFocus Else gCn.Execute "insert into stuinfo(roll,name,marks) values(" & _ Val(Trim(txtroll.Text)) & ",'" & Trim(txtname.Text) & "'," & _ Val(Trim(txtmarks.Text)) & ")" If gCn.Errors.Count = 0 Then Call DisplayAllStudents '--->''calling the sub-routine so that you can ensure the new record has been added. MsgBox "New student added." txtroll.Text = "" txtname.Text = "" txtmarks.Text = "" txtroll.SetFocus End If End If End Sub just creating connection with the database and nothing else. avoid putting any DML statements here Private Sub Form_Load() gCn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;" & _ "User ID=sa;Initial Catalog=sonia" gCn.Open Call DisplayAllStudents End Sub take a listview control. for this select Microsoft Common Controls 6.0 (SP3) from components list. you can access it via Project->Components it will fetch all existing student records from your table Public Sub DisplayAllStudents() Dim str As String Dim rs As New ADODB.Recordset Dim li As ListItem str = "select * from stuinfo order by roll" rs.CursorLocation = adUseClient rs.CursorType = adOpenDynamic rs.LockType = adLockOptimistic rs.Open str, gCn lvstudents.ListItems.Clear If rs.RecordCount > 0 Then rs.MoveFirst While Not rs.EOF() With lvstudents Set li = .ListItems.Add(, , (rs!roll)) li.SubItems(1) = rs!Name li.SubItems(2) = rs!marks End With rs.MoveNext Wend End If If rs.State = adStateOpen Then rs.Close Set rs = Nothing End Sub
regards
Shouvik
Last edited by choudhuryshouvi; Apr 17th, 2008 at 3:10 am.
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Have a problem? Don't worry just give me a call and I'll fix it for you.
•
•
•
•
hey one more thing tell me that Mastering in Visual basic 6.0 is good for freshers nor not.
It is not too highfy book na, dat freshers like me can’t understand. Cz I m planning to purchase dat book.
it will help you. but i think it has some sections where explanations are a little bit advanced for freshers.
hv a nice day.
regards
Shouvik
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Have a problem? Don't worry just give me a call and I'll fix it for you.
•
•
Join Date: Mar 2008
Posts: 324
Reputation:
Solved Threads: 7
hey Shouvik ,Till Now I have did the foll--
I inserted the records and as the records are inserted into the database,all the textboxes are clear.
THX FOR UR HELP.
Now I m trying to do myself DELETION, UPDATION, SEARCHING the Records.
The eg,u have given me in ur previous reply is
GRIDVIEW DATABINDING--Rite????
Till Now,I understood that
U write Connection string as procedure.
When the User clicks on Insert button further msg box is displayed.
If yes,Records are inserted.
If no,Focus is set to textbox Roll.
And To Clear The textboxes I did-
Public Sub clear()
Dim txt As Control
For Each txt In Controls
If TypeOf txt Is TextBox Then
txt.Text = ""
End If
Next
End Sub
I inserted the records and as the records are inserted into the database,all the textboxes are clear.
THX FOR UR HELP.
Now I m trying to do myself DELETION, UPDATION, SEARCHING the Records.
The eg,u have given me in ur previous reply is
GRIDVIEW DATABINDING--Rite????
Till Now,I understood that
U write Connection string as procedure.
When the User clicks on Insert button further msg box is displayed.
If yes,Records are inserted.
If no,Focus is set to textbox Roll.
And To Clear The textboxes I did-
Public Sub clear()
Dim txt As Control
For Each txt In Controls
If TypeOf txt Is TextBox Then
txt.Text = ""
End If
Next
End Sub
Last edited by sonia sardana; Apr 17th, 2008 at 7:40 am.
•
•
•
•
hey Shouvik ,Till Now I have did the foll--
I inserted the records and as the records are inserted into the database,all the textboxes are clear.
THX FOR UR HELP.
Now I m trying to do myself DELETION, UPDATION, SEARCHING the Records.
ok...try those and if you need any help then plz create a new thread for that.
•
•
•
•
The eg,u have given me in ur previous reply is
GRIDVIEW DATABINDING--Rite????
•
•
•
•
Till Now,I understood that
U write Connection string as procedure.
When the User clicks on Insert button further msg box is displayed.
If yes,Records are inserted.
If no,Focus is set to textbox Roll.
the msgbox's purpose is exactly same what you have said.
•
•
•
•
And To Clear The textboxes I did-
Public Sub clear()
Dim txt As Control
For Each txt In Controls
If TypeOf txt Is TextBox Then
txt.Text = ""
End If
Next
End Sub
so what is the summery?
now you are successfully adding your data into the database or not?
have my efforts showed you some ways or not???
reply...
regards
Shouvik
Last edited by choudhuryshouvi; Apr 17th, 2008 at 2:43 pm.
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Have a problem? Don't worry just give me a call and I'll fix it for you.
•
•
•
•
hey Shouvik,Till Now i do not want GRIDVIEW DATABINDING, I am doing if user types Rollno 1 in textbox Roll and click on search button, then in the corresponding textboxes info is displayed in other textboxes like his name,& his marks.
In VB there is no DataReader.
but so what? there are still so many methods that you can use to read your records from the database.
so you wish to try the search code yourself?
ok.....go ahead....i'm always here for you.
for searching you can apply one of the these two techniques :-
1. Exact Record Searching
2. Dictionary Searching
choose which will be best suited for you.
regards
Shouvik
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Have a problem? Don't worry just give me a call and I'll fix it for you.
![]() |
Similar Threads
- Database Connectivity in C (C)
- Cannot find server or DNS Error - please help! (Viruses, Spyware and other Nasties)
- Connectivity (C++)
- connectivity in Borland C (C++)
- Ms Sql Server 2000 (MS SQL)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Help with Login scree developed in visual basic 6.0
- Next Thread: Creating Alert in Vb.6
| Thread Tools | Search this Thread |
Tag cloud for Visual Basic 4 / 5 / 6
* 6 429 2007 access activex add age append application basic beginner birth bmp calculator cd cells.find click client code college column component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report retrieve save search sendbyte sites sort sql sql2008 sqlserver subroutine table tags textbox time timer urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows





