473,686 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use Function to pass query parameter?

I need a way to create a table with a programmaticall y defined name. I have a
Make Table query that will create the table with the name that I put in the
query, but I don't know how to (or if I can) use a function to pass the table
name into the query.

Here is the basic form of the Mke Tble query:

SELECT TxDate, Amount, Tx_ID, TxAcctName, INTO [table name here] FROM
tblTxJournal;

This does not seem to work:

SELECT TxDate, Amount, Tx_ID, TxAcctName, INTO (QryPrmName) FROM
tblTxJournal;

Public Function QryPrmName() As String
Dim strTxAcct As String
Dim strQtr As String
Dim strYr As String
strTxAcct = Forms!frm2!cbx2 TxAcct.Column(1 )
strQtr = Forms!frm2!cbx2 Quarter.Column( 1)
strYr = Forms!frm2!cbx2 Year
QryPrmName = "Transactions_F or_" & strTxAcct & "_" & strQtr & "_" & strYr
End Function

Is it possible to do this?

Thanks in advance.
Nov 12 '05 #1
15 7180
To insert rows into an existing table, you must use INSERT INTO, not SELECT
.... INTO. If you want to specify the target fields for your insert, you list
them in parentheses right after INSERT INTO <table-name> in the same order
they will appear in the output from the SELECT.

Example:
INSERT INTO tblFoo (FooName, FooDescription) SELECT BarName, BarDescription
FROM tblBar

If you want to create the table with the correct field names directy rather
than creating it first, but still have control over the field names, you just
need to alias the fields in your select list. Of course, you'll have to alter
the table design afterward if you need an Autonumber, indexes, aetc.

Example:
SELECT BarName As FooName, BarDescription As FooDescription
INTO tblFoo FROM tblBar

On Sat, 31 Jan 2004 21:57:24 GMT, "deko" <dj****@hotmail .com> wrote:
I need a way to create a table with a programmaticall y defined name. I have a
Make Table query that will create the table with the name that I put in the
query, but I don't know how to (or if I can) use a function to pass the table
name into the query.

Here is the basic form of the Mke Tble query:

SELECT TxDate, Amount, Tx_ID, TxAcctName, INTO [table name here] FROM
tblTxJournal ;

This does not seem to work:

SELECT TxDate, Amount, Tx_ID, TxAcctName, INTO (QryPrmName) FROM
tblTxJournal ;

Public Function QryPrmName() As String
Dim strTxAcct As String
Dim strQtr As String
Dim strYr As String
strTxAcct = Forms!frm2!cbx2 TxAcct.Column(1 )
strQtr = Forms!frm2!cbx2 Quarter.Column( 1)
strYr = Forms!frm2!cbx2 Year
QryPrmName = "Transactions_F or_" & strTxAcct & "_" & strQtr & "_" & strYr
End Function

Is it possible to do this?

Thanks in advance.


Nov 12 '05 #2
Hi and thanks for the reply.

well, perhaps I could just rename the table after creating it:

For Each tdf In CurrentDb.Table Defs
If tdf.Name = strOldName Then
'CurrentDb.Exec ute "DROP TABLE " & strNewName
tdf.Name = strNewName
End If
Next
CurrentDb.Table Defs.Refresh

the problem is dealing with table if it already exists... I'm also having
trouble with:

DoCmd.OutputTo acOutputTable, strNewName

I can get it to work but getting ugly errors...

The other option, as you suggested, is to create the table first with the right
name and insert into it. I was thinking I could use DDL for this:

DoCmd.RunSql ("CREATE TABLE strNewName (TxDate Date, Amount Currency, TxAcctName
Text, Tx_ID AutoIncrement Constraint PrimaryKey PRIMARY KEY);

or something like that...

"Steve Jorgensen" <no****@nospam. nospam> wrote in message
news:ls******** *************** *********@4ax.c om...
To insert rows into an existing table, you must use INSERT INTO, not SELECT
... INTO. If you want to specify the target fields for your insert, you list
them in parentheses right after INSERT INTO <table-name> in the same order
they will appear in the output from the SELECT.

Example:
INSERT INTO tblFoo (FooName, FooDescription) SELECT BarName, BarDescription
FROM tblBar

If you want to create the table with the correct field names directy rather
than creating it first, but still have control over the field names, you just
need to alias the fields in your select list. Of course, you'll have to alter
the table design afterward if you need an Autonumber, indexes, aetc.

Example:
SELECT BarName As FooName, BarDescription As FooDescription
INTO tblFoo FROM tblBar

On Sat, 31 Jan 2004 21:57:24 GMT, "deko" <dj****@hotmail .com> wrote:
I need a way to create a table with a programmaticall y defined name. I have aMake Table query that will create the table with the name that I put in the
query, but I don't know how to (or if I can) use a function to pass the table
name into the query.

Here is the basic form of the Mke Tble query:

SELECT TxDate, Amount, Tx_ID, TxAcctName, INTO [table name here] FROM
tblTxJournal ;

This does not seem to work:

SELECT TxDate, Amount, Tx_ID, TxAcctName, INTO (QryPrmName) FROM
tblTxJournal ;

Public Function QryPrmName() As String
Dim strTxAcct As String
Dim strQtr As String
Dim strYr As String
strTxAcct = Forms!frm2!cbx2 TxAcct.Column(1 )
strQtr = Forms!frm2!cbx2 Quarter.Column( 1)
strYr = Forms!frm2!cbx2 Year
QryPrmName = "Transactions_F or_" & strTxAcct & "_" & strQtr & "_" & strYr
End Function

Is it possible to do this?

Thanks in advance.

Nov 12 '05 #3
On Sat, 31 Jan 2004 21:57:24 GMT, deko wrote:
I need a way to create a table with a programmaticall y defined name. I have a
Make Table query that will create the table with the name that I put in the
query, but I don't know how to (or if I can) use a function to pass the table
name into the query.

Here is the basic form of the Mke Tble query:

SELECT TxDate, Amount, Tx_ID, TxAcctName, INTO [table name here] FROM
tblTxJournal;

This does not seem to work:

SELECT TxDate, Amount, Tx_ID, TxAcctName, INTO (QryPrmName) FROM
tblTxJournal;

Public Function QryPrmName() As String
Dim strTxAcct As String
Dim strQtr As String
Dim strYr As String
strTxAcct = Forms!frm2!cbx2 TxAcct.Column(1 )
strQtr = Forms!frm2!cbx2 Quarter.Column( 1)
strYr = Forms!frm2!cbx2 Year
QryPrmName = "Transactions_F or_" & strTxAcct & "_" & strQtr & "_" & strYr
End Function

Is it possible to do this?

Thanks in advance.


If a QueryDef object is not required, then doing the following would
simplify things (modify the error handle to suit your needs)...

Public Function QryPrmName() As String
On Error Goto Error_Handler
Dim strTxAcct As String
Dim strQtr As String
Dim strYr As String
Dim strSQL As String
Dim db As Database
strTxAcct = Forms!frm2!cbx2 TxAcct.Column(1 )
strQtr = Forms!frm2!cbx2 Quarter.Column( 1)
strYr = Forms!frm2!cbx2 Year
strSQL = "SELECT TxDate, Amount, Tx_ID, TxAcctName, INTO "
strSQL = strSQL & "Transactions_F or_" & strTxAcct & "_" & strQtr & "_"
strSQL = strSQL & "strYr" FROM tblTxJournal;
db.Execute sqlStr, dbFailOnError

Exit_Here:
Exit Function

Error_Handler:
MsgBox Err.Number & " - " & Err.Description
resume Exit_Here

End Function
--
Mike Storr
veraccess.com
Nov 12 '05 #4
OK, is it just that you need to know how to get the table name into the SQL
Statement? Just use the .Execute method of the database object, and insert
the name into the SQL text.

Dim strSQL As String
Dim dbs As DAO.Database

strSQL = "SELECT ... INTO [" & <mytablename> & "] ..."

Set dbs = CurrentDB
dbs.Execute strSQL, dbFailOnError
Set dbs = Nothing

On Sat, 31 Jan 2004 21:57:24 GMT, "deko" <dj****@hotmail .com> wrote:
I need a way to create a table with a programmaticall y defined name. I have a
Make Table query that will create the table with the name that I put in the
query, but I don't know how to (or if I can) use a function to pass the table
name into the query.

Here is the basic form of the Mke Tble query:

SELECT TxDate, Amount, Tx_ID, TxAcctName, INTO [table name here] FROM
tblTxJournal ;

This does not seem to work:

SELECT TxDate, Amount, Tx_ID, TxAcctName, INTO (QryPrmName) FROM
tblTxJournal ;

Public Function QryPrmName() As String
Dim strTxAcct As String
Dim strQtr As String
Dim strYr As String
strTxAcct = Forms!frm2!cbx2 TxAcct.Column(1 )
strQtr = Forms!frm2!cbx2 Quarter.Column( 1)
strYr = Forms!frm2!cbx2 Year
QryPrmName = "Transactions_F or_" & strTxAcct & "_" & strQtr & "_" & strYr
End Function

Is it possible to do this?

Thanks in advance.


Nov 12 '05 #5
> Just use the .Execute method of the database object, and insert
the name into the SQL text.


I think db.Execute is the way to go, but...
I'm getting this error:

"Error Number 3061: Too Few Parameters..."

Is this one of those cases where I have to use a Function to get the query
parameter?
I have stuff like this in the Where clause:

WHERE (((tblTxJournal .TxType_ID = Forms!frm2!cbx2 TxType Or Forms!frm2!cbx2 TxType
= 0) And (tblTxJournal.T xAcct_ID = Forms!frm2!cbx2 TxAcct Or
Forms!frm2!cbx2 TxAcct = 0)) Or (Forms!frm2!cbx 2Year = "<All>")) ...

It seems to work fine in a stored query, but barfs with db.Execute
Nov 12 '05 #6
DoCmd.RunSql seems to be working - apparently db.Execute cannot resolve the
references

Thanks for the help!
Nov 12 '05 #7
That's right. I didn't realize your query was trying to use Access object
references as parameters. To do this using DAO, you have to populate the
references from code which means you have to use a QueryDef object.

Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
dim strSQL As String

strSQL = "... WHERE Abc=prmAbc ..."

Set dbs = CurrentDB
Set qdf = dbs.CreateQuery def("", strSQL)
qdf.Parameters( "prmAbc") = Forms!<form-name>!<control-name>
qdf.Execute dbFailOnError
Set qdf = Nothing
Set dbs = Nothing

I recommend doing this, not RunSQL because direct DAO is more efficient, and
has better error handling.

On Sun, 01 Feb 2004 02:12:54 GMT, "deko" <dj****@hotmail .com> wrote:
DoCmd.RunSql seems to be working - apparently db.Execute cannot resolve the
references

Thanks for the help!


Nov 12 '05 #8
> I recommend doing this, not RunSQL because direct DAO is more efficient, and
has better error handling.


A QueryDef object may be the best solution for a situation like this, but RunSql
seems to be working, too...

I have to join 4 tables and get parameters from 4 different combo boxes to get
the recordset I need. The objective is to narrow a datasheet of transactions by
type, account, date, etc. and export the filtered records to a file. At this
point I'm setting up a CreateTable Sql string with VBA, running it with RunSql,
then dumping the table contents into a file with OutputTo, then dropping the
table with db.Execute.

The reason the table name is important is because OutputTo, when used with
minimal arguments, uses the table name as the default file and header name. So
I take stuff like AcctNumber, Type, Date, CustID, etc., format it into a string
and use that as the table name. This makes the file self-describing. The nice
thing is that OutputTo gives you a dialog that lets you choose the type of file
you want to export to - Excel, Html, Rtf, XML, etc. - when you leave off the
arguments.
Nov 12 '05 #9
On Sun, 01 Feb 2004 06:16:14 GMT, "deko" <dj****@hotmail .com> wrote:
I recommend doing this, not RunSQL because direct DAO is more efficient, and
has better error handling.
A QueryDef object may be the best solution for a situation like this, but RunSql
seems to be working, too...


Yes, RunSQL works, but expetiance shows that it has some problems, and I
suggest you start learning to use QueryDef instead. For one thing, RunSQL's
behavior is dependent upon the Warnings setting. If Warnings are on, the user
will be asked to confirm or cancel if there is a problem such as a primary key
violation. If Warnings are off, any such problems will be ignored, all rows
with no problems will be inserted, and the user will enver know this happened.
if ever want your code to be able to detect and report this kind of error
itself, you'll simply have to use .Execute.
I have to join 4 tables and get parameters from 4 different combo boxes to get
the recordset I need. The objective is to narrow a datasheet of transactions by
type, account, date, etc. and export the filtered records to a file. At this
point I'm setting up a CreateTable Sql string with VBA, running it with RunSql,
then dumping the table contents into a file with OutputTo, then dropping the
table with db.Execute.

The reason the table name is important is because OutputTo, when used with
minimal arguments, uses the table name as the default file and header name. So
I take stuff like AcctNumber, Type, Date, CustID, etc., format it into a string
and use that as the table name. This makes the file self-describing. The nice
thing is that OutputTo gives you a dialog that lets you choose the type of file
you want to export to - Excel, Html, Rtf, XML, etc. - when you leave off the
arguments.


Well, it's the name of the table or the query if you export a query. You
could create a named querydef, set the SQL to SELECT * FROM <table-name>, then
export the querydef. That's much more efficient than copying all the data to
a new table, and is actually a bit simpler to code.
Nov 12 '05 #10

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

Similar topics

13
2732
by: | last post by:
From ASP I run a query using MIN(some_field/parameter) Now I need to pass this parameter to the query from ASP code How do I do that? Syntax like MIN( / ) does not work
5
14036
by: Belinda | last post by:
Hello All I have the following test.asp page which needs one parameter querystr but my querystr is a very long string value. When I send a long value the query string is getting truncated after some characters. Can you please kindly share the code segment to workaround how to pass such a long string value to a asp page. This is how I invoke the test page:
13
3487
by: Maroon | last post by:
Hi, I want to write a standard java user defined function, like this example:- select db_fun("roll"), roll from student; **db_fun() will work for all tables of the all databse here db_fun is the function that i want to write. The student table is:- roll name 1 'A'
5
13216
by: Don Seckler | last post by:
I have an update query that runs when a report closes. I have several reports that will need to run the update query with diferent criteria. I'd like to simply make the criteria change in the report vba instead of making different queries. Here's my query sql: UPDATE Draw SET Draw.Billed = Yes WHERE (((Draw.Billed)=No) AND ((Draw.WholesalerName)="Hudson"));
7
21618
by: Zlatko Matiæ | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the server ? Is it possible to use parameters in pass-through queries ? An additional question: Is it possible to connect to a database on MySQL or PostgreSQL using ADO ? Is it possible to execute pass-through queries with parameters, using ADO...
23
1764
by: Neil | last post by:
I have an Access 2000 MDB file with a SQL 7 back end. I have a main table with 50,000 records; and I have a selections table with 50,000 records for each machine that uses the database (about 25-50). This allows each user to have their own set of selections. The selections table has three fields: ID (int), Sel (bit), MachName (varchar). ID and MachName comprise the primary key. I have a view that combines the main table and the entries...
9
2312
by: Alan Silver | last post by:
Hello, I'm a bit surprised at the amount of boilerplate code required to do standard data access in .NET and was looking for a way to improve matters. In Classic ASP, I used to have a common function that was included in all pages that took an SQL query and returned a disconnected recordset. This meant that data access could be achieved in a single line. I would like to do something similar in ASP.NET. I know I could just duplicate...
1
7751
by: | last post by:
Hi, I've defined an ObjectDataSource and a parameterized DataSet. I would like it if I could pass the parameter value that describes the query that creates the DataSet as part of a user control declaration. Here's how the parameter is described in the context of my ObjectDataSource: <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetArticlesByWhomever" TypeName="WhateverTableAdapters.WhateverTableAdapter">
0
2191
by: alpha122 | last post by:
I am beginner and I need some help writing simple perl script. I need to get url query and pass it to the javascript function as parameter. for example: my page will be http://myhomepage.html?var1=test1&var2=test2.. These variable could be anything that is entered by user. Now I need to get these variables an pass those values in my javascript function as parameter. Thanks in advance for help..
0
8585
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
8517
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
9057
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
8780
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...
0
7603
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
6441
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
5800
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();...
1
2947
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
1940
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.