473,797 Members | 3,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unskilled VB coding = createQueryDef doesn't work.

6 New Member
Howdy all. I have pretty much NO VB/Access coding experience, but am forced to come up with a generic script/process for users with varying database designs and varying levels of expertise to use and integrate into their Dbs.

Following is my script and is launched On_Open event. The script determines someone's desired upload class (test, archive or recent), executes a specific query based on that info, then writes the query results to a text file, tab-delimited. I am trying to use createQueryDef so that the query can be built on the fly with a temporary query - no need to make several permanent queries. As you might notice, the main problem is having to insert TOP 300 in the SQL query, the WHERE changes are quite manageable, the TOP 300 gets me.

Anyway, my diagnostic MsgBox right before the createQueryDef works, but I get stuck in some kinda error/hold after that, that requires ctrl+break to get out of.

Thanks for any insight!!! I am using Access 2000 right now, to ensure as much universal compatibility as possible, but that may be a mistake!

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private Sub Report_Open(Cancel As Integer)
  4.  
  5.     Dim uploadClass As Integer
  6.     Dim countDates As Integer
  7.  
  8.     Set dbs = CurrentDb
  9.     Set rst = dbs.OpenRecordset("SELECT * FROM date_tracking")
  10.  
  11.     If rst.RecordCount = 0 Then
  12.         response = MsgBox("Is this for a Test Upload?", vbYesNoCancel, "Upload Type")
  13.         If response = vbYes Then
  14.             uploadClass = 0
  15.             bp (uploadClass)   'run query and data export
  16.         ElseIf response = vbNo Then
  17.             response = MsgBox("Access will now generate an Archive upload.", vbOKCancel, "Upload Type")
  18.             If response = vbOK Then
  19.                 uploadClass = 2
  20.                 bp (uploadClass)
  21.             End If
  22.         End If
  23.     Else: response = MsgBox("Access will now generate an upload of new records since last.", vbOKCancel, "Upload Type")
  24.         If response = vbOK Then
  25.             uploadClass = 1
  26.             bp (uploadClass)
  27.         End If
  28.     End If
  29.     Set rst = Nothing
  30. End Sub
  31.  
  32. Sub bp(uploadClass As Integer)
  33.     On Error GoTo bp_Err
  34.  
  35.         Dim date_update As Integer
  36.         Dim fileName As String
  37.         Dim strTop As String
  38.         Dim strWhere As String
  39.         Dim strFields As String
  40.         Dim qryName As String
  41.         Dim strSQL As String
  42.  
  43.         date_update = 1
  44.  
  45.         '********   COPY AND PASTE FROM SQL VIEW OF Query *********
  46.         '----------------------------------------------------------------------------------------------------
  47.  
  48.         strFields = " { copy & paste complex JOIN/ON/FROM query } "
  49.  
  50.         '----------------------------------------------------------------------------------------------------
  51.         '*******************************************************************
  52.  
  53.         If (uploadClass = 0) Then
  54.             strTop = "TOP 300 "
  55.             strWhere = " WHERE table1.date > Date()-1095 "
  56.             date_update = 2
  57.         ElseIf (uploadClass = 1) Then
  58.             strTop = ""
  59.             strWhere = " WHERE (((table1.date)<(SELECT Max([UploadDate]) AS maxDate FROM date_tracking;))) "
  60.         Else
  61.             strTop = ""
  62.             strWhere = " WHERE table1.date > Date()-1095 "
  63.         End If
  64.  
  65.         strSQL = "SELECT " & strTop & strFields & strWhere
  66.         qryName = "bpQuery"
  67.         Set dbs = CurrentDb
  68.         response = MsgBox("I'm about to CreateQueryDef", vbOKOnly, "") ' for diagnostic purposes only
  69.         Set qdf = dbs.CreateQueryDef(qryName, strSQL)
  70.         response = MsgBox("I'm just went through CreateQueryDef", vbOKOnly, "") ' for diagnostic purposes only
  71.         Set qdf = Nothing
  72.  
  73.         fileName = "C:/bp/" & DatePart("m", Date) & DatePart("d", Date) & DatePart("yyyy", Date) & ".txt"
  74.        DoCmd.OutputTo acOutputQuery, qryName, acFormatTXT, fileName, False, ""
  75. '    OR    DoCmd.TransferText acExportDelim, "", qryName, fileName, False, ""       
  76.  
  77.        DoCmd.QueryDefs(qryName).delete
  78.  
  79. bp_Exit:
  80.         If date_update = 1 Then
  81.             Set rst = dbs.OpenRecordset("date_tracking")
  82.                 rst.AddNew
  83.                 rst![UploadDate] = Date - 1
  84.                 rst.Update
  85.                 rst.Close
  86.         End If
  87.  
  88. bp_Err:
  89.         date_update = 2
  90.         Resume bp_Exit
  91. End Sub
  92.  
Regards,
oh4real
Oct 3 '07 #1
2 2935
oh4real
6 New Member
BTW, line 65-70 is where the createQueryDef is.

oh4real
Oct 4 '07 #2
oh4real
6 New Member
Nevermind. Problems were at least 2-fold:

1) Tools -> References - were not all ticked, as per forum helpers! Don't remember which ones I ticked, so can't test if this was the original problem, but I think #2 is the real reason.

2) The QueryDefs.delet e qryName line had 'db' for database, while it should been 'dbs'. Therefore the query was never deleted - this caused a primary failure of the CreateQueryDef (qryName, strSql) since it couldn't overwrite.

3) The _Err & _Exit had not Exit Sub in them, so got unending loops - once did .addnew 245,000 times ;)

Clear head, great night's sleep and refill on Adderall go a long way...

Sorry to bother,
oh4real
Oct 4 '07 #3

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

Similar topics

5
8862
by: Andrew | last post by:
Hi: I run into the following problem where I would use CreateQuerydef to create a new Table and then use "Execute" to execute the statement. However, I keep getting an error message saying that the table already exists! (Error 3010 Table already exists). But I made sure that the table were dropped before I executed the statement and I went into the database to check that the table was deleted before I ran the "execute" statement. Here...
3
4814
by: priscilla.jenkins | last post by:
Alright, I'm really new to SQL and VBA and all this, so I might be completely off course...but just tell me. I know C and Assembly, but that doesn't help me much here. I'm trying to create a module that creates 5 queries (that a report is based on) based on a table that the user defines. My tables are named by date (not my choice), for example: -ArchivedActions05-20-05 Everything involving that seems fine. I'm getting a syntax error...
144
6979
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this initiative. Typically I find that coding standards are written by some guy in the company who has a way of coding that he likes and then tries to force everybody else to write code the way he likes it, not for any rational reason, but simply for the...
1
1709
by: R Reyes | last post by:
Hello All, I'm always looking for ways to improve my code. Most of the time (whenever I'm working on a project) I write a bunch of functions. Then after the project is finished, I put all the similar functions into their own module/class/page for neatness: -- like a stringsManipulate class which has all string manipulation methods to the project...separated from the whole project into its own library for neatness. -- like a...
14
1755
by: Jon Maz | last post by:
Hi, I am writing inline code .aspx pages using VS.Net 2002. The color coding seems to work for VB.NET inline, and not for C# inline (see below). Does anyone know how to get this working for C#? TIA, JON
20
1672
by: TJ Doherty | last post by:
Need help understanding the following please: When I am creating a project and code my connection using Dim connectString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Temp\NW-test.MDB", everything works correctly when I run the project. However, if I set up the exact same connection using the DataAdapter wizard and try to run the project, I get a "Microsoft Jet database engine cannot open the file..." error.
8
1356
by: Electrified Research | last post by:
I just want to know other peoples' views about this. Now that VS2005 is out, how do people feel about visual editors? Personally I am so comfortable with hand coding that Visual Studio along with other comparable tools seem to slow me down or just don't provide enough value to what I'm doing. I truly hope I'm missing the point and really want to discover something about Visual Studio that makes me want to switch, but until then, I just...
8
3535
by: Expat | last post by:
I have several queries that I create from SQL statements using Currentdb.CreateQueryDef in one of these cases the code breaks and refuses to create the query giving me a: Run-time error 3131. Syntax error in FROM clause. I'm supposing that it won't create the query because it analyzes the SQL string and determines that there is a syntax error in the FROM clause of the query I am trying to create. The odd thing is that if I take this same SQL...
6
2494
by: rahuldev999 | last post by:
Hi I am beginner in the access project. can anyone suggest how to deal with this " Set rq = Db.CreateQueryDef("r_temp_rslt_research", strSql)" in the below code.As its a MDB application which needs to be converted into ADP. Any kind of help would be highly appreciated. Below is the snippet of code: Dim Db As DAO.Database
0
9537
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
10469
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
10246
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
9066
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7560
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
6803
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
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3750
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2934
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.