Log in

View Full Version : Solved: insert query



gibbo1715
10-20-2005, 01:52 PM
Can anyone tell me how to amend my query please to account for the fact that my first field is an autonumber

Thanks

Gibbo


strSQL = "INSERT INTO tbl_Test VALUES ('" & _
Me.TextBox1 & "', '" & _
Me.TextBox2 & "', '" & _
Me.TextBox3 & "', '" & _
Me.TextBox4 & "', '" & _
Me.TextBox5 & "')"
cn.Execute strSQL

xCav8r
10-20-2005, 02:29 PM
Add the column names to your SQL statement for which you're inserting values, but omit the field with the autonumber. It'll be incremented automatically. The same applies for any columns that have default values that you don't want to override. Example:

CREATE TABLE tblExample
(
lngAutoNumber AUTOINCREMENT,
strFirstName TEXT(30),
strLastName TEXT(50),
lngNumber INT
);

INSERT INTO tblExample (strFirstName, strLastName, lngNumber)
VALUES ('John', 'Smith', 66);

Li'l helper routine:

Option Compare Database
Option Explicit

'Long names
Function SingleEnquotedString(StringToEnquote As String) As String
SingleEnquotedString = "'" & StringToEnquote & "'"
End Function

'Abbreviations
Function EnqStr1(Data As String) As String
EnqStr1 = "'" & Data & "'"
End Function

Sub DoStuff()
Dim strSQL As String
strSQL = "INSERT INTO tblExample (strLastName, strFirstName, lngNumber)" _
& vbNewLine & "VALUES (" _
& EnqStr1(Me.TextBox1.Value) & "," & vbNewLine _
& EnqStr1(Me.TextBox2.Value) & "," & vbNewLine _
& EnqStr1(Me.TextBox3.Value) & ");"
End Sub

gibbo1715
10-20-2005, 03:58 PM
Thats looks really good

I ll have a play tomorrow

Thanks for looking

Gibbo

gibbo1715
10-20-2005, 03:59 PM
Also can you tell me the meaning of Option Compare Database as its a new one on me

Cheers

Gibbo

xCav8r
10-20-2005, 08:06 PM
http://support.microsoft.com/default.aspx?scid=kb;en-us;98227

HTH! :)

gibbo1715
10-21-2005, 12:41 AM
Thanks works great