473,387 Members | 1,517 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,387 software developers and data experts.

Unskilled VB coding = createQueryDef doesn't work.

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 2895
BTW, line 65-70 is where the createQueryDef is.

oh4real
Oct 4 '07 #2
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.delete 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
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...
3
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...
144
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...
1
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...
14
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...
20
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...
8
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...
8
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...
6
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...

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.