473,382 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Check for records before inserting variables using SQL Statement

Hello, I am trying to check to see if records exists before inserting them into a table with a sql statement. I am using variables retrieved from a form and I can insert new records fine but when I check to see if the record exist I get a runtime error 3129 Invalid SQL Statement Expected Delete, Insert, etc... Any help would be greatly appreciated. This is the first time I have been at a complete loss.

Here is my code (Resulting SQL String below):



Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub Command9_Click()
  3. Dim AddDays As Date
  4. Dim sqlstring As String
  5.  
  6.  
  7. DoCmd.SetWarnings False
  8.  
  9. AddDays = Me.txtBeginDate
  10.  
  11. For AddDays = Me.txtBeginDate To Me.txtEndDate
  12.  
  13. sqlstring = "if NOT EXISTS (SELECT ExpenseDate, ExpenseReportID " & _
  14. "FROM tblExpenseDetail " & _
  15. "WHERE ExpenseDate = " & "#" & AddDays & "#" & " AND ExpenseReportID = " & Me.txtExpenseReportID & ") " & _
  16. "Insert Into tblExpenseDetail (ExpenseDate, ExpenseReportID, ExpenseHotel) " & _
  17. "Values (#" & AddDays & "#, " & ExpenseReportID & ", " & ExpenseHotel & "); "
  18.  
  19. MsgBox sqlstring
  20.  
  21. DoCmd.RunSQL sqlstring
  22.  
  23.  
  24. AddDays = DateAdd("d", 0, AddDays)
  25.  
  26. Next AddDays
  27.  
  28. DoCmd.SetWarnings True
  29.  
  30. End Sub
  31.  
  32.  
  33.  
Resulting in:

if NOT EXISTS (SELECT ExpenseDate, ExpenseReportID FROM tblExpenseDetail WHERE ExpenseDate = #4/9/2011# AND ExpenseReportID = 99) Insert Into tblExpenseDetail (ExpenseDate, ExpenseReportID, ExpenseHotel) Values (#4/9/2011#, 99, 89);
Apr 30 '11 #1

✓ answered by RuralGuy

You need a separate SELECT query or Domain Function to test the record first.

3 3131
RuralGuy
375 Expert 256MB
You need a separate SELECT query or Domain Function to test the record first.
May 1 '11 #2
Thanks for the tip, I used dcount to check for the records and that did the trick. I was just stuck on trying to do it all with a single SQL statement and your tip got me out of that train of thought
May 3 '11 #3
RuralGuy
375 Expert 256MB
Excellent! Thanks for posting back with your success and glad we could help.
May 3 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Dariusz | last post by:
I have a problem where I need to pass two variables using GET from a form I have, to solve a page selection problem I have. The code is written that if a new visitor arrives at the front page of...
0
by: Marko Poutiainen | last post by:
Situation: We had to make our SQLServer 2000 database multi-lingual. That is, certain things (such as product names) in the database should be shown in the language the user is using (Finnish,...
6
by: Raqueeb Hassan | last post by:
Hello There! Have you guys tried inserting variables in mySQL tables? Do I have to use 'quote' as we had been doing to insert strings? mysql> INSERT INTO occurrence (word_id,page_id) VALUES...
3
by: Wim Verhavert | last post by:
Hi all, I'm bothered with this question for weeks now.... Is it possible to edit multiple records at once using VBA? I have this continuous form and depending on the selection the user makes, I...
5
by: Bill Priess | last post by:
Hey gang, Ok, I'm stumped on this one... I am using the using statement to wrap a SqlDataAdapter that I am using to fill a DataTable. Now, what I need to know is, just how much block-scope...
1
by: Kepler | last post by:
I have a situation where I need to use a Using statement that creates some records in a database. After that completes, if it completes, I need to do some file creation. Any code I'm putting...
8
by: J-T | last post by:
I have a class like below I have a couple of questions about that: 1) I like to use "Using statement" when creating an object of this class,so I had to implement IDisposable.Am I doing this right...
5
by: Andreas Müller | last post by:
Hi, I was wondering, if there is something similar in VB.NET like the using statement in C#. What it does is to automatically call Dispose on the object decrared with in the statement when the...
23
by: Tony Johansson | last post by:
Hello! I just wonder what is the point of having the reader variable declared as TextReader in the snippet below.. Is it because of using the polymorfism on the reader variable perhaps. using...
2
by: tshad | last post by:
I tried to put multiple objects in one using statements like so: using (fs = new FileStream(fromImagePath + "\\" + (string)dr, FileMode.Open, System.IO.FileAccess.Read) , BinaryReader br = new...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.