I have an Excel spreadsheet with 2000 rows of data. I need to create a SQL script to insert the data into a table. Below is the formula I used in Excel and it works for creating the insert statement, but how do I "Not Insert" if DiagnosisCode already exists?
= "INSERT INTO CommonDiagnosis (DiagnosisCode, Description, ActiveThru, ActiveThruEnteredBy, DiagnosisFormat) VALUES('" & A3 & "', '" & B3 & "', '" & C3 & "', '" & D3 & "', '" & E3 & "')"
You could try using an IF statement:
IF NOT EXISTS
( SELECT DiagnosisCode
FROM CommonDiagnosis
)
BEGIN
INSERT INTO CommonDiagnosis (DiagnosisCode,Description,ActiveThru,ActiveThruEnteredBy,DiagnosisFormat)
VALUES ('"&A3&"','"&B3&"','"&C3&"','"&D3&"','"&E3&"')
END;
Related
I'm trying to realise a simple sign up structure, and I am really new in VBA and Access.
At the instruction INSERT TO it should add a record to the table USER. but it adds 2 identical records that have just a different userID, generated by the Automatic Numeration. Then I want to copy the last generated UserID in global variable valID, so that I can insert this UserID in a another table.
What am I doing wrong?
DoCmd.RunSQL ("INSERT INTO User ([Type], [FiscalCode], [Name], [Surname], [Birthdate], [Gender], [phoneNumber], [email], [country], [city], [postalCode], [street], [houseNumber])VALUES ('" & valtype & "', '" & valfis & "', '" & valname & "', '" & valsur & "', '" & valdata & "', '" & valgen & "', '" & valphone & "', '" & valemail & "','" & valcountry & "', '" & valcity & "', '" & valcode & "', '" & valstreet & "', " & valnum & ")")
valID = DLookup("[UserID]", "User", "[FiscalCode] = '" & valfis & "'")
after this code I want to do another instruction of the type insert to in a different table to insert in this new table as primary key the UserId I just obtained. this is the only other INSERT INTO inside the code.
For this to happen, most likely form is bound to table and controls are bound to fields so user is inputting data into controls and then code runs INSERT as well thereby creating two records of same data.
A record is committed to table when one of the following happens:
1) table or query or bound form is closed
2) move to another record
3) run code to save
Remove the code.
Use form/subform arrangement for inputting parent record as well as dependent record in 'another' table.
I am still developing my School management system. I want to create a multiple username and password for my system. But I am getting a Runt ime error 31314 on the Insert statement.
CurrentDb.Execute "INSERT INTO users(Username,Password,Email,Mobile,UserType) VALUES('" & Me.uname & "', '" & Me.upass & "', '" & Me.uemail & "', '" & Me.umobile & "', '" & Me.utype & "',)"
You can/should use a temporary parameter query:
The sql statement looks much more clear
It takes care of datatypes (no 's necessary)
It prevents SQL Injection
With CurrentDb().CreateQueryDef(vbNullString, _
"INSERT INTO users (Username, [Password], Email, Mobile, UserType) VALUES (p1, p2, p3, p4, p5)")
.Parameters("p1").Value = Me.uname
.Parameters("p2").Value = Me.upass
.Parameters("p3").Value = Me.uemail
.Parameters("p4").Value = Me.umobile
.Parameters("p5").Value = Me.utype
.Execute dbFailOnError
End With
If you want you can rename the parameters p1 to p5 to more describing names i.e. pUName and so on.
You have a trailing comma here:
"INSERT INTO users(Username,Password,Email,Mobile,UserType) VALUES('" & ... & Me.utype & "',)"
Trailing commma ---------------------------------------------------------------------------^
You should also surround the Password field with square brackets, since this is a reserved word in MS Access:
INSERT INTO users (Username, [Password], Email, Mobile, UserType)
I am getting the error above saying that I cannot explicitly define the identity column without doing these things however, I am not trying to set the value of the identity column. I am merely trying to execute a basic insert statement using user input. I'm really stuck and need to get past this. Thoughts?
Public Sub ExecuteSQL(ByVal sql As String)
Using myConnection As New Data.SqlClient.SqlConnection(connString)
myConnection.Open()
Using cdmDatabaseCommand As New Data.SqlClient.SqlCommand(sql, myConnection)
cdmDatabaseCommand.ExecuteNonQuery()
End Using
End Using
End Sub
Dim insertSpray As String = ""
insertSpray = "INSERT INTO Spray VALUES ('" & sprayDate & "','" & timeStart & "','" & timeFinish & "','" & tankVolumeStart & "','" & tankVolumeFinish & "','" & comment & "','" & vehicleId & "','" & vehicleTime & "','" & siteType & "','" & area & "','" & applicatorOneId & "','" & applicatorTwoId & "','" & sprayLocationFeature & "','" & sprayStartLocationDescription & "','NULL','NULL','NULL','NULL','" & sprayEndLocationDescription & "');"
ExecuteSQL(insertSpray)
You're not specifying a column-list, so SQL Server assumes you want to insert into all columns, in table-order.
You need to change your syntax to specify the names of the columns you're inserting into (and leave out the identity column, obviously):
insert into Spray (SprayDate, TimeStart, ...)
values (...
Also note that your code is vulnerable to SQL injection attacks. You should look into using parameterized queries.
Try to explicitly make the columns of table Spray without the identity column.
Something like this:
insertSpray = "INSERT INTO Spray(Column1, Column2,...) VALUES (Value1,Value2,..)"
I have two tables, one table called Act_Reg and the other is Active_Pay. I have two queries: one query is a view in MS Access and it gives me the result as Paycoach :
SELECT Act_Reg.member_id, Active_Pay.date_pay, Active_Pay.kind_sport,
Active_Pay.kind_prac, Active_Pay.coach, Active_Pay.tuition, Active_Pay.discount
FROM Act_Reg
INNER JOIN Active_Pay ON Act_Reg.member_id = Active_Pay.member_id;
The second query I use in VB6 for getting the result of query one:
rstemp1.Open "SELECT sum(tuition)-sum(discount) FROM paycoach where date_pay Between '" & Trim(txtdatein.text) & "' And '" & Trim(txtdateto.text) & "' and coach='" & Cbocoach.text & "' and kind_sport='" & cbosport.text & "' and kind_prac='normal' group by tuition", db, adOpenKeyset, adLockOptimistic
I want to calculate the payment for each coach, according to paid membership and discount in a date range. The two queries work well, but one query is in MS Access as view and the second is in VB6.
How can I combine these two queries into one query, which I can use in Visual Basic 6?
I think you can do just what you ask by doing exactly how you describe it.
It's probably not the most efficient, but this should work by simply inserting your Access query SQL directly into your VB6 query SQL
your paycheck query SQL
SELECT Act_Reg.member_id, Active_Pay.date_pay, Active_Pay.kind_sport,
Active_Pay.kind_prac, Active_Pay.coach, Active_Pay.tuition, Active_Pay.discount
FROM Act_Reg
INNER JOIN Active_Pay ON Act_Reg.member_id = Active_Pay.member_id;
Your rs.Open sql
"SELECT sum(tuition)-sum(discount) FROM paycoach where date_pay Between '" & _
Trim(txtdatein.text) & "' And '" & Trim(txtdateto.text) & "' and coach='" & _
Cbocoach.text & "' and kind_sport='" & cbosport.text & "' and kind_prac='normal' group by tuition"
Modify your rs.open to use Combined statement
Dim sql as string
sql = "SELECT sum(tuition)-sum(discount) FROM "
sql = sql & "(SELECT Act_Reg.member_id, Active_Pay.date_pay, Active_Pay.kind_sport, "
sql = sql & "Active_Pay.kind_prac, Active_Pay.coach, Active_Pay.tuition, Active_Pay.discount "
sql = sql & "FROM Act_Reg INNER JOIN Active_Pay ON Act_Reg.member_id = Active_Pay.member_id) "
sql = sql & "As paycoach "
sql = sql & "where date_pay Between '"
sql = sql & Trim(txtdatein.text) & "' And '" & Trim(txtdateto.text) & "' and coach='"
sql = sql & Cbocoach.text & "' and kind_sport='" & cbosport.text & "' and kind_prac='normal' group by tuition"
rstemp1.Open sql, db, adOpenKeyset, adLockOptimistic
I'm using VB6 and MS Access. my sql command is
insert into BatchInfo (BName,BDate,Currency) values('" & Me.txtBatchName.Text & "','" & Me.dtpBatchDate.Value & "','" & Me.cboCurrency.Text & "')
the output of the command at run time is
"insert into BatchInfo (BName,BDate,Currency) values('batch1','8/2/2012','AED')"
here is the schema of the BatchInfo Table
BatchID AutoNumber
BName Text
BDate Date/Time
Currency Text
I cannot find any syntax error. Please help.
Currency is a reserved word, escape it thusly;
insert into BatchInfo (BName, BDate, [Currency]) values (...
MS Access typically likes # signs around its dates:
insert into BatchInfo (BName,BDate,Currency)
values('" & Me.txtBatchName.Text & "','#" & Me.dtpBatchDate.Value & "#','" & Me.cboCurrency.Text & "')
You are using single quotes, not double quotes.
Try this
insert into BatchInfo (BName,BDate,Currency) values(""" & Me.txtBatchName.Text & """,#" & Me.dtpBatchDate.Value & #",""" & Me.cboCurrency.Text & """)