473,779 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Change a query's SQL code from excel

Good day all, I'm a newish VBA coder who was taught from Google Groups
- so thanks to you all very much!

I have run into a minor problem, I hope you can give me some help ...
I'm running office 2003 for this one.

In excel I have a program running ... and based on the results of an
optionbutton (called "Form2.Radio_Al lParameters") I want to change the
SQL that runs a query in access. If the button is True, the SQL should
read "WHERE (((ChemistryRea dings.UsedDate) =Yes) AND
((ChemistryRead ings.UsedLocati on)=Yes) AND ((MOE_Tables1to 6.Used)<>No
Or (MOE_Tables1to6 .Used) Is Null) AND
((MOE_Standard_ Definitions.Use d)<>No Or
(MOE_Standard_D efinitions.Used ) Is Null));"

but when the button is False it should read ".....AND
((MOE_Tables1to 6.Used=Yes) AND...."

The reasons that I think that I want to change the SQL and not create
a new query are: (1) it is a crosstab query that's built off of this
query that is what is imported into excel and (2) I don't know how to
create the relationships from excel.

My excel VBA snippet is like this:

'--------------------------------code start-------------------------
'Public Sub cmdGo_Click()

'Declarations
Dim DB As DAO.Database
Dim RS As DAO.Recordset
Dim FLD As DAO.Field
Dim qryDef1 As DAO.QueryDef
Dim strSQL As String

'Recall filepath and open database
FileName = Sheets("README" ).Cells(1, 20).Value
Set DB = DAO.OpenDatabas e(FileName)
'---------------------------------------------------------------------------------
'Re-create the query "ChemistryQuery " based on whether you want ALL
the parameters (including those not in the standards) or not:
Set qryDef1 = DB.QueryDefs("C hemistryQuery")

If Form2.Radio_All Parameters = True Then
strSQL = "SELECT [ChemistryReadin gs].[Parcel] & ' ' &
[Location] & ' ' & [LaboratoryID] & ' ' & [LaboratoryJobNu mber] & ' '
& [SampleDate] AS Header, Parameter_Names .[CAS#], MOE_Tables1to6.
[Parameter ID], ChemistryReadin gs.Parameter,
MOE_Tables1to6. StandardName, MOE_Standard_De finitions.ID,
MOE_Tables1to6. Standard, MOE_Tables1to6. Units AS MOE_Tables1to6_ Units,
MOE_Tables1to6. Notes, ChemistryReadin gs.SampleDate,
ChemistryReadin gs.Reading, ChemistryReadin gs.Units AS Reading_Units,
ChemistryReadin gs.Comments, ChemistryReadin gs.UsedDate,
ChemistryReadin gs.UsedLocation , MOE_Tables1to6. Used,
MOE_Standard_De finitions.Used FROM MOE_Standard_De finitions RIGHT JOIN
((Parameter_Nam es RIGHT JOIN ChemistryReadin gs ON
Parameter_Names .ParameterName = ChemistryReadin gs.Parameter) LEFT JOIN
MOE_Tables1to6 ON ChemistryReadin gs.Parameter =
MOE_Tables1to6. Parameter) ON MOE_Standard_De finitions.FullN ame =
MOE_Tables1to6. StandardName" & _
"WHERE (((ChemistryRea dings.UsedDate) =Yes) AND
((ChemistryRead ings.UsedLocati on)=Yes) AND ((MOE_Tables1to 6.Used)<>No
Or (MOE_Tables1to6 .Used) Is Null) AND
((MOE_Standard_ Definitions.Use d)<>No Or
(MOE_Standard_D efinitions.Used ) Is Null));"
Else
strSQL = "SELECT [ChemistryReadin gs].[Parcel] & ' ' &
[Location] & ' ' & [LaboratoryID] & ' ' & [LaboratoryJobNu mber] & ' '
& [SampleDate] AS Header, Parameter_Names .[CAS#], MOE_Tables1to6.
[Parameter ID], ChemistryReadin gs.Parameter,
MOE_Tables1to6. StandardName, MOE_Standard_De finitions.ID,
MOE_Tables1to6. Standard, MOE_Tables1to6. Units AS MOE_Tables1to6_ Units,
MOE_Tables1to6. Notes, ChemistryReadin gs.SampleDate,
ChemistryReadin gs.Reading, ChemistryReadin gs.Units AS Reading_Units,
ChemistryReadin gs.Comments, ChemistryReadin gs.UsedDate,
ChemistryReadin gs.UsedLocation , MOE_Tables1to6. Used,
MOE_Standard_De finitions.Used FROM MOE_Standard_De finitions RIGHT JOIN
((Parameter_Nam es RIGHT JOIN ChemistryReadin gs ON
Parameter_Names .ParameterName = ChemistryReadin gs.Parameter) LEFT JOIN
MOE_Tables1to6 ON ChemistryReadin gs.Parameter =
MOE_Tables1to6. Parameter) ON MOE_Standard_De finitions.FullN ame =
MOE_Tables1to6. StandardName" & _
"WHERE (((ChemistryRea dings.UsedDate) =Yes) AND
((ChemistryRead ings.UsedLocati on)=Yes) AND ((MOE_Tables1to 6.Used)<>No
Or (MOE_Tables1to6 .Used) Is Null) AND
((MOE_Standard_ Definitions.Use d)=Yes);"
End If

qryDef1.Execute strSQL
-------------------end code-----------------
My problem is that I am unsure of IF I can change the SQL of the query
at all ..... but also how to do so if I can (the last line is what is
giving me the error called "runtime error 3421 data type conversion
error".

Thank you very much for any help that you can supply, I will monitor
this thread super closely in case I should have provided any more
information.

Chris
Feb 12 '08 #1
0 1579

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4201
by: Mike Knight | last post by:
(I've also posted this problem on microsoft.public.excel.programming) I have a MS Access 2003 Database named "AS400 Fields.mdb". This database contains links to tables on an AS400. In MS Excel 2003, I have VBA code that creates and executes queries using the Access database, and returns the results to an Excel sheet. The first time the query is executed, results are returned to Excel in usually less than 10 seconds. However, if the...
3
11932
by: John | last post by:
Is there a way to code the button that's available in the query window--microsoft excel icon that exports to excel. I know transferspreadsheet will do this---but I want the query, which is in a pivot table view, to be exported as a pivot, not just a data list. Does this code exist? Is it transferspreadsheet but with a twist?
0
2924
by: ghanley | last post by:
I have searched the web all day for a lead on this. I have found how to control the Graph object mut not the embedded excel unbound object frame. I am trying to chart the data below on one chart that comes from a query. I have managed to do this in excel and embedding the worksheet. Program 2005 2006 2007 2008 2009 2010
1
6506
by: Jaime Leivers | last post by:
Here's a median function in access that you can call in any query. You could change this to any excel function you wanted. Most people can find the windows help file that says how to call an excel function but don't know how to pass an array of the recordset they made into that function. This uses GetRows that nicely creates an array that can be passed into excel. I have this data table for testing. The table name is TestData ...
19
12770
by: wreckingcru | last post by:
I'm trying to output a SQL query that is constructed thru my VB.net GUI into an excel file. Here is the code I'm using: 'Sqlstmt is the SQL query statement 'Conn is the SQL Connection object cmd = New SqlCommand(Sqlstmt, Conn) datareader = cmd.ExecuteReader()
3
4342
by: perryche | last post by:
All, In excel, when you find a field within a spreadsheet is wrong, and every row needs to be changed, you simply change one cell, copy that cell, and select all the rest of the cell, and paste it. In word, you can just replace... in Access, how do we change fields in a table? especially if it is a check box. Say, I want all the field to be yes for a selected (or even all) records. How to accomplish this easier than export all these...
4
2512
by: torontolancer | last post by:
Hi there how r u .I would really appriciate ur concern regarding checking out this code. its beind a command button and i am have a combo box selecing a query criteria and then pressing the button to take care recoredset from access to excel. i have checked the code in any ways its working fine but when i use the combo box criteria forms!formname!combobox in the query the code breaks down at rst.open a """""""""""gives...
1
10506
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am trying to export to Excel using a command in an Access Form. RowID strFY AccountID CostElementWBS 1 2008 1 7 2 2008 1 7 I want to...
3
4588
by: Bface | last post by:
Hi all, Hope everyone had a good holiday. I am having a difficult time changing the date format of a field from Excel. I have never had this problem before. I link the excel spreadsheet to my DB, from the spreadsheet I have an append query that appends only certain fields from the spreadsheet to a table. One of the fields is called . The date in this field are displayed as ‘yyyymmdd’ ex:20070525 To change date format I usually create an...
0
9633
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
10305
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...
1
10074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8959
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
7483
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
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2867
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.