473,800 Members | 2,497 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why does my INSERT INTO code not work?

1 New Member
First, apologies for all the code; I'm not sure what is most relevant to my problem.

I am trying to use the code below to select Records from an Access table called [Client_Details] and insert them into another table (in the same database) called [ClientMailMerge Selection]. Once there I will then run a mail merge operation.

The problem is, no Records are being inserted and no error message is ever displayed.

I have inherited the code and am effectively stuck with it, so suggestions for obvious errors in the existing code preferred please. I have little knowledge of SQL or VBA

Oh, and I am using Access 2002, but the database is formatted as Access 2000.

Many thanks for any help you can offer.

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnSearch_Click()
  2. Dim rcsLookUp As Recordset
  3. Dim SQLOrderBy As String
  4. Dim lCount As Long
  5.  
  6.     On Error GoTo Err_btnSearch_Click
  7.     DoCmd.Hourglass True
  8.     DoEvents
  9.  
  10.     SQLQ = "INSERT INTO ClientMailMergeSelection "
  11.     SQLQ = SQLQ & "(CaseID, REF, E_DATE, SNAME, INITIAL, INITIAL1, TITLE, "
  12.     SQLQ = SQLQ & "Sal_Too, Sal_Dear, ADD1, ADD2, ADD3, ADD4, PCODE, "
  13.     SQLQ = SQLQ & "TEL, Email, HouseNumber, Flat, HouseName, StreetLine1 )"
  14.     SQLQ = SQLQ & "SELECT "
  15.     SQLQ = SQLQ & "Client_Details.CaseID, Client_Details.REF, Client_Details.E_DATE, Client_Details.SNAME, "
  16.     SQLQ = SQLQ & "Client_Details.INITIAL, Client_Details.INITIAL1, "
  17.     SQLQ = SQLQ & "Client_Details.TITLE, Client_Details.Sal_Too, "
  18.     SQLQ = SQLQ & "Client_Details.Sal_Dear, Client_Details.ADD1, "
  19.     SQLQ = SQLQ & "Client_Details.ADD2, Client_Details.ADD3, "
  20.     SQLQ = SQLQ & "Client_Details.ADD4, Client_Details.PCODE, "
  21.     SQLQ = SQLQ & "Client_Details.TEL, Client_Details.Email, "
  22.     SQLQ = SQLQ & "Client_Details.HouseNumber, Client_Details.Flat, "
  23.     SQLQ = SQLQ & "Client_Details.HouseName, Client_Details.StreetLine1 "
  24.  
  25.     SQLQ = SQLQ & "FROM Client_Details "
  26.  
  27.     SQLWhere = ""
  28.  
  29.     If ComboLender <> "" Then
  30.         SQLWhere = "LenderID = " & ComboLender.Column(0) & " "
  31.     End If
  32.  
  33.     ' status
  34.     If Not IsNull(ComboStatus) Then
  35.         If SQLWhere <> "" Then SQLWhere = SQLWhere & "AND "
  36.         SQLWhere = SQLWhere & " Client_Details.[STATUS] = '" & ComboStatus & "' "
  37.     End If
  38.  
  39.     If ComboMedia <> "" Then
  40.         If SQLWhere <> "" Then
  41.             SQLWhere = SQLWhere & "AND "
  42.         End If
  43.         SQLWhere = SQLWhere & "Media_ID = " & ComboMedia.Column(0) & " "
  44.     End If
  45.  
  46.     ' security type
  47.     If Not IsNull(ComboSec) Then
  48.         If SQLWhere <> "" Then SQLWhere = SQLWhere & "AND "
  49.         SQLWhere = SQLWhere & "[client_details].[sec] = " & ComboSec & " "
  50.     End If
  51.  
  52.     If txtDateFrom <> "01/01/1998" Or txtDateTo <> Date Then
  53.         ' only do date checking if theye have changed the dates otherwise
  54.         ' no point we just get em all
  55.         If Not IsNull(txtDateFrom) And Not IsNull(txtDateTo) Then
  56.             If Not IsDate(Me.txtDateFrom) Then
  57.                 MsgBox "Invalid 'From' date", vbOKOnly + vbExclamation
  58.                 Me.txtDateFrom.SetFocus
  59.                 Exit Sub
  60.             End If
  61.             If Not IsDate(txtDateTo) Then
  62.                 MsgBox "Invalid 'To' date", vbOKOnly + vbExclamation
  63.                 txtDateTo.SetFocus
  64.                 Exit Sub
  65.             End If
  66.             If SQLWhere <> "" Then SQLWhere = SQLWhere & "AND "
  67.             Select Case Frame51.Value
  68.             Case 1
  69.                 SQLWhere = SQLWhere & "[E_Date] "
  70.             Case 2
  71.                 SQLWhere = SQLWhere & "[Statusdate] "
  72.             Case 3
  73.                 SQLWhere = SQLWhere & "[Date2] "
  74.             End Select
  75.             SQLWhere = SQLWhere & "BETWEEN #" & Format(txtDateFrom, "mm/dd/yyyy") & "# "
  76.             SQLWhere = SQLWhere & "AND #" & Format(txtDateTo, "mm/dd/yyyy") & "# "
  77.         End If
  78.     End If
  79.  
  80.     If SQLWhere <> "" Then
  81.         SQLWhere = "WHERE " & SQLWhere
  82.     End If
  83.     SQLOrderBy = "ORDER BY REF"
  84.  
  85.     CurrentDb.Execute "DELETE * FROM ClientMailMergeSelection", dbFailOnError
  86.     CurrentDb.Execute SQLQ & SQLWhere & SQLOrderBy, dbFailOnError
  87.     Me.ClientMailMergeSelection.Form.Requery
  88.     DoEvents
  89.     DisplayHeadings
  90.     DoCmd.Hourglass False
  91.     DoEvents
  92.     Exit Sub
  93.  
  94. Err_btnSearch_Click:
  95.     MsgBox Err.DESCRIPTION
  96.     Resume Next
  97.  
  98. End Sub
Sep 25 '09 #1
2 3381
NeoPa
32,579 Recognized Expert Moderator MVP
I think we can help some. There may be a few things to deal with though, including some work for you to do finding info (we'll help here too of course).

I'll start with some advice about preparing and posting your code. If after trying to compile you find any issues you cannot deal with then let us know (with details) and we''l help you sort it out.
It is always a good idea to ensure that variable name checking is enabled, AND your code compiles (at least compilation has been attempted), before submitting a question (Require Variable Declaration).

This avoids asking questions which are much more easily resolved on your own PC than on a forum.

To ensure variable name checking is enabled for all new modules, go to - Tools / Options / Editor (from the VBA Editor window) and set Require Variable Declaration to True (checked). For existing modules, ensure that the Option lines at the very top include :
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
Sep 26 '09 #2
NeoPa
32,579 Recognized Expert Moderator MVP
Moving on, and assuming that you had no problems with the earlier post and have changed your code accordingly, let's look at the SQL that's created within your code and run it from within a QueryDef (saved Access query). That way, purely for testing/debugging purposes, we can see what the error messages may be. If there is none, then we can at least explore further by tweaking it more easily.

What I want you to do is to stop the code at line #86 (See Debugging in VBA for help with this) and display the contents of the variable SQLQ into the Immediate Pane of the Visual Basic Editor window. I can't see what type of variable SQLQ is (this was the main reason for my earlier post btw). When it is displayed in there I would like you to Copy it into the clipboard and Paste it into a new QueryDef (Create a new QueryDef - Insert \ Query from the Menu, Select SQL View - View \ SQL - from the menu, Paste in the value from SQLQ you copied earlier). If you run this from the Database window then you should see some messages displaying what is happening. Post this here so that we can give it some consideration.
Sep 26 '09 #3

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

Similar topics

31
9828
by: lawrence | last post by:
I'm not sure how this is normally done, on a large site, perhaps one running Phorum. Occassionally a thread will have hundreds of entries, perhaps a meg or two worth of data. You won't necessarily print all that to the screen, but PHP has to hold it in memory. Run some operations on it, or, more likely, have an array that you keep adding things to, and very soon you run into the 8 meg limit that is the default limit for PHP scripts. ...
14
4302
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to salvage the records from the many table and without going into detail, one of the reasons I can't do the opposite as there are records in the ONE table that I need to keep even if they don't have any child records in the MANY table. Below I created...
3
1791
by: nicver | last post by:
I am fixing a client's Web site and for some reason an ASP class does not want to use the variables it retrieves when it initialiases. This is an excerpt of the class and it is enough to show what is not working correctly: ------ Class clsAd private pintMyData1 private pintMyData2 private pintMyData3
3
3954
by: mp | last post by:
I have example like follows: OleDbCommand InsertCommand = new OleDbCommand("INSERT INTO RecnikTable VALUES (@ENGLESKI, @SRPSKI)", thisConnection); InsertCommand.Parameters.Add("@ENGLESKI", OleDbType.VarChar); InsertCommand.Parameters.Add("@SRPSKI", OleDbType.VarChar); string reci = {textBox2.Text, textBox2.Text}; InsertCommand.Parameters.Value=reci;
1
2370
by: nicholas | last post by:
To insert a record in a Ms Access database and be able to retrieve the newly created ID (autonumber) I used the code below (code 1). Now, the problem is that this is not very secure and that, if for example an insertion contains a ' or a " this code fails. It is much better to work with @parameters. So could someone change my code to make it work with @parameters and that I still can retrieve that autonumber. (note that it is for a MS...
8
2026
by: Howard Kaikow | last post by:
I got bored today, so I decided to rewrite the code in KB article 316383 to decrease the number of object references. This resulted in a number of nested With ... End With. The original code had a Dim r As Integer, c As Integer shortly before a For Next.
9
3705
by: anachronic_individual | last post by:
Hi all, Is there a standard library function to insert an array of characters at a particular point in a text stream without overwriting the existing content, such that the following data in appropriately moved further down? From a cursory search of the libc documentation I can't find such a function. Will I have to write it myself? Thanks.
1
2189
by: Iain | last post by:
I have the following .net page with a pushbutton in a datagrid. This pushbutton should delete the selected record. It works in the same manner in many other pages of the same format (I actually copied and pasted most of this code from another form). Can anyone enlighten me please as to why this happens randomly and how to evercome it as It is driving me nuts. Thanks in advance for any assistance offered
5
3469
by: =?Utf-8?B?bXBhaW5l?= | last post by:
Hello, I am completely lost as to why I can't update a DropDownList inside a DetailsView after I perform an insert into an object datasource. I tried to simply it down to the core demostration: default.aspx:
2
1441
daJunkCollector
by: daJunkCollector | last post by:
Hey, I have a simple web app that requires the user to click a text link to add a section to the form. As you will see from the following code (PHP/mySQL), the link inserts a row into the database table. The problem is that, during testing, sometimes when I click the link, two rows are added instead of only one. I only want one row to be added. Could you please help me figure out my bug? // Insert and delete actions for Education, Work...
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10507
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10279
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7582
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6815
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.