Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Data into Database

Status
Not open for further replies.

WJProctor

Programmer
Jun 20, 2003
151
0
0
GB
Hi there, just started to use .net. Thing is i can view data from the database which is really good, but i cant add, the code must be wrong somewhere, i dont know why, could someone have a look and tell me if they can spot anything.

Public OpenConnection As OleDbConnection
Public DataAdapter As OleDbDataAdapter
Public DataCommand As OleDbCommand

Public Sub Connect()
Dim DBStr As String
Dim DBStr2 As String
DBStr = GetSetting ("Login", "DBSettings", "DBString")
DBStr2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Trim(DBStr) & ";Persist Security Info=False"
OpenConnection = New OleDbConnection(DBStr2)
OpenConnection.Open()
End Sub

Public Sub InsertUser(ByRef Username As String, ByRef Password As String, ByRef Firstname As String, ByRef Surname As String, ByRef Notes As String, ByRef AccessLevel As String, ByRef ForcePassword As String, ByRef LastPasswordChange As String, ByRef Enabled As Boolean, ByRef Deleted As Boolean)
Dim SQLStr As String
Call Connect()
SQLStr = "INSERT INTO Users VALUES('" & Trim(Username) & "','" & Trim(Password) & "','" & Trim(Firstname) & "','" & Trim(Surname) & "','" & Trim(Notes) & "','" & Trim(AccessLevel) & "','" & Trim(ForcePassword) & "','" & Trim(LastPasswordChange) & "','" & Trim(Enabled) & "','" & Trim(Deleted) & "')"
MsgBox(SQLStr)
DataCommand = New OleDbCommand(Trim(SQLStr), OpenConnection)
DataCommand.ExecuteNonQuery()
Call Disconnect()
End Sub

Sorry the code is a bit long, maybe someone could point out the problem, well i hope so or you could tell me how i should be doing it.

Kind Regards

JP
 
I think your INSERT INTO Statement is incorrect. It should be something like this...

"INSERT INTO TABLENAME (" & strFields & ") VALUES (" & strValues & ")", objConn)


strFilds = Field names in the table.

Stve
 
Its the OpenConnection that it pick up on mainly
 
Take a look at faq709-1526

It's for VB5 & 6, but many of the same topics are applicable to VB.NET

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top