0

New to vb and tried the below code. searched for solutions in google. But im confused with the results.So im posting in SO.

Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cmd As ADODB.Command
Dim statement As String


Private Sub cmd_update_Click()
con.Open "Provider=SQLOLEDB;Data Source=STPLSVR06.syrmatech.local;Initial Catalog=BVM;Database=Bluestar;uid=sa;pwd=STPLadmin098;"
rs.Open = "insert into Test_Duct values(txt_Board_SrNo.Text,txt_Test_DateTime.Text,txt_User_Name.Text,txt_Communication_status.Text,txt_WatchDog_Status.Text,txt_VoltMes.Text,txt_VoltStatus.Text)"

rs.Close
con.Close
End Sub

I used this code from a video and I get an compiler error in "rs.open" statement. I will be thankful if any of u can give an good insert command or help in finding whats the error.

suvi
  • 37
  • 1
  • 8
  • I know. Its wrong But im not in a situation to get a book. now. I need to finish it soon. its a order from my boss. Im sorry. But plz help how to insert into table.. – suvi Oct 27 '15 at 07:47
  • 1
    Why book? There is help for ADO, online, like here - https://msdn.microsoft.com/en-us/library/ms678086(v=vs.85).aspx ; there are numerous good samples (not videos!) with explanations. And if you need 'finish it soon', but have no idea about code, then just hire some knowledgeable developer. – Arvo Oct 27 '15 at 07:52
  • Thanks @Arvo. but i just need know how to use insert command. and need to know what I tried is a waste? – suvi Oct 27 '15 at 07:58
  • 1
    Search for ADO connection methods, allowing to execute any SQL command. What you tried, is not waste - but usually it is much better first to understand, what you need and what options any framework/API gives to you and only then start to use (or copy-paste) these methods. – Arvo Oct 27 '15 at 11:04
  • Possible duplicate of [VBA, ADO.Connection and query parameters](http://stackoverflow.com/questions/10352211/vba-ado-connection-and-query-parameters)? – C-Pound Guru Oct 27 '15 at 18:58

1 Answers1

1

Val only saves numbers. Try this:

Dim usrname As String 'declare as a string

brdSrNo = txt_Board_SrNo.Text
usrname = txt_User_Name.Text
rs.Open "insert into vbtest values(" & brdSrNo & ",'" & usrname & "')", con,adOpenDynamic, adLockBatchOptimistic

'string should be given in single quotes

Remember to add "adOpenDynamic, adLockBatchOptimistic" this just a small help from my knowledge. But try to learn before proceeding.

Kopika
  • 122
  • 15