I am new in vba and specially vba query. My question is following:-
I have a form named "frmCreateQry" which have following
FieldType FieldName
1. ComboBox cboSelectTblQry
2. ListBox lstSelectFrom
3. ListBox lstSelectTo
4. Button cmdMove
5. Button cmdExport
cboSelectTblQry rowsource have form Onload value. It contains all table names of my database. When I select any table name from this combobox then all related fields name display in lstSelectFrom (ListBox). If I select any fields from lstSelectFrom and click on cmdMove (Button) then selected fields moves to lstSelectTo.
My question is that I want to create query using lstSelectTo items/fields by clicking cmdExport (Button) and export this query to excel format.
Kindly tell me the process.
Thanks
9 3936
You will need to loop through your list box's selected items and concatenate them into a string separated by commas. Then just get the name of the table from your combo box and you can build the query SQL string. To export it, I'm pretty sure that you have to then pass the SQL string to a querydef, save it and then export the query.
I prepared this... but not working - Dim dbs As Database
-
Dim qdf As DAO.QueryDef
-
Dim Tbl As DAO.TableDef
-
Dim varItem As Variant
-
Dim strCriteria As String
-
Dim strSQL As String
-
-
Set dbs = CurrentDb()
-
Set qdf = dbs.CreateQueryDef("MyQry")
-
Set Tbl = dbs.CreateTableDef(Me.cboSelectTblQry.Value)
-
For Each varItem In Me!lstSelectTo.ItemsSelected
-
strCriteria = strCriteria & "," & Me!lstSelectTo.ItemData(varItem) & ""
-
Next varItem
-
If Len(strCriteria) = 0 Then
-
MsgBox "You did not select anything." _
-
, vbExclamation, "Nothing to find!"
-
-
Exit Sub
-
End If
-
-
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
-
strSQL = "SELECT* FROM Tbl "
-
-
DoCmd.OpenQuery "MyQry"
-
Set db = Nothing
-
Set qdf = Nothing
can you tell me the code as you described....
After line 21, put Debug.Print strCriteria and post the result (it will display in the immediate window. Press Ctrl + G if it isn't visible.). Then, your strSQL isn't accurate. Assuming that strCriteria is the WHERE clause, it would need to be - strSQL = "SELECT * FROM " & Me.cboSelectTblQry & " WHERE " & strCriteria
Then you need to edit the SQL of a query. - Set qdf = db.QueryDef("MyQry")
-
qdf.SQL = strSQL
-
qdf.Close
Now you can export the query. I use the DoCmd.TransferSpreadsheet method for which you can get the instructions from the MSDN website.
debug.print strCriteria
result
Emp_Design,Emp_Gender,Emp_Lname,Emp_FName,ID,Offic e,UserDA
but error " Method or Data member not found" with following line - Set qdf = dbs.QueryDef("MyQry")
My Full code is:- - Dim dbs As Database
-
Dim qdf As DAO.QueryDef
-
Dim varItem As Variant
-
Dim strCriteria As String
-
Dim strSQL As String
-
-
Set dbs = CurrentDb()
-
-
For Each varItem In Me!lstSelectTo.ItemsSelected
-
strCriteria = strCriteria & "," & Me!lstSelectTo.ItemData(varItem) & ""
-
Next varItem
-
-
If Len(strCriteria) = 0 Then
-
MsgBox "You did not select anything." _
-
, vbExclamation, "Nothing to find!"
-
Exit Sub
-
End If
-
-
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
-
Debug.Print strCriteria
-
strSQL = "SELECT * FROM " & Me.cboSelectTblQry & " WHERE " & strCriteria
-
-
Set qdf = dbs.QueryDef("MyQry")
-
qdf.sql = strSQL
-
qdf.Close
-
-
Set dbs = Nothing
Hello seth,
I resolve previous error.. Now syntex error "3075"
i attached image for same here.
Sorry it has taken me so long to get back with you.
Now that you have resolved the previous error, please put a Debug.Print strSQL on line 22 and post back what it puts in the immediate window. Based on the error that you are receiving, there is a problem with your SQL string.
I create the following simple Code Segment that will do exactly as you requested and will also allow for Spaces in Table and Field Names. For the sake of brevity and conciseness, I included neither Validation or Error Checking: - Dim strTableName As String
-
Dim strBaseSQL As String
-
Dim strCriteria As String
-
Dim varItem As Variant
-
Dim strSQL As String
-
Dim qdf As DAO.QueryDef
-
-
strTableName = Me![cboSelectTblQry]
-
strBaseSQL = "SELECT "
-
-
For Each varItem In Me![lstSelectTo].ItemsSelected
-
strCriteria = strCriteria & "[" & Me![lstSelectTo].ItemData(varItem) & "],"
-
Next
-
-
strSQL = strBaseSQL & Left$(strCriteria, Len(strCriteria) - 1) & " FROM [" & strTableName & "]"
-
-
Set qdf = CurrentDb.CreateQueryDef("MyQry", strSQL)
-
-
RefreshDatabaseWindow
-
-
'All you have to do is Export MyQry here
Thanks to Seth and ADezii...
ADezii code work fine after some editing.
But now one more problem is that...
After export in excel to MyQry, Date fields shows numerical format instead of Date Format in excel but date field shows correct date format in MyQry in query section.
Any Suggestion..
My Code is following:- -
If cboFormat = "Excel" Then
-
Dim strTableName As String
-
Dim strBaseSQL As String
-
Dim strCriteria As String
-
Dim varItem As Variant
-
Dim strSQL As String
-
Dim qdf As DAO.QueryDef
-
Dim OutPut As String
-
-
-
For Each qdf In CurrentDb.QueryDefs
-
If qdf.Name = "MyQry" Then
-
DoCmd.DeleteObject acQuery, "MyQry"
-
Exit For
-
End If
-
Next
-
-
strTableName = Me![cboSelectTblQry]
-
strBaseSQL = "SELECT "
-
-
For Each varItem In Me![lstSelectTo].ItemsSelected
-
strCriteria = strCriteria & "[" & Me![lstSelectTo].ItemData(varItem) & "],"
-
Next
-
-
If Len(strCriteria) = 0 Then
-
MsgBox "You did not select anything." _
-
, vbExclamation, "Nothing to find!"
-
Exit Sub
-
End If
-
-
strSQL = strBaseSQL & Left$(strCriteria, Len(strCriteria) - 1) & " FROM [" & strTableName & "]"
-
-
Set qdf = CurrentDb.CreateQueryDef("MyQry", strSQL)
-
-
OutPut = "D:/Export.xlsx"
-
DoCmd.TransferSpreadsheet acExport, , "MyQry", OutPut
-
MsgBox " File has been exported to " & OutPut
-
-
RefreshDatabaseWindow
-
End If
-
-
ExitSub:
-
Exit Sub
-
ErrorHandler:
-
Resume ExitSub
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Paxton |
last post by:
In the same way that you can create and alter permanent tables in
Access 2000 using SQL in ASP ( CREATE TABLE <tablename> col_1(col_1
datatype), col_2(col_2 datatype....Col_n(col_n datatype)) is...
|
by: Valerian John |
last post by:
I have a ListBox webcontrol on an aspx page. Items are added to the
ListBox using client-side code. However, when the page is posted back the
items are missing/not available. (It is like the...
|
by: Stimp |
last post by:
I have a listbox of values that I populate from a database.
I want the user to be able to re-order the list
(by first selecting an item and then clicking 'up' or 'down' buttons)
and then save...
|
by: Baren |
last post by:
Hi!
I have a generalized Stored Procedure to get the listbox items in a
datareader.Then i am binding the datareader to the listbox. For different
pages and different conditions i need to hide...
|
by: forbes |
last post by:
Hi,
I have a user that used the Query Wizard to create a query in Access.
Now she claims that her master table is missing all the data that was
excluded from the query. Can you create anything...
|
by: Brian Simmons |
last post by:
Hi All,
I search on codeproject and google but didn't find what I was looking for.
Anybody know of a good implementation where you have 2 listboxes and you
want to move items between the 2.
...
|
by: Refugnic |
last post by:
I tried to fill a ListBox with a DataSource pointing to an ArrayList.
It all works fine...up to one point.
The ArrayList is dynamic, which means the contents of it change, during the course of...
|
by: kimiraikkonen |
last post by:
Hi,
I can read from a text file and insert text file's contents into a
listbox whose format is line by line using this code:
Dim reader As String
reader = My.Computer.FileSystem.ReadAllText("c:...
|
by: divya |
last post by:
Hello,
I used the below code to move selected items between two listboxes
Left and Right:-
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="TestPopup.aspx.cs" Inherits="TestPopup" %>
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
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...
|
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,...
|
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,...
|
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: 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...
|
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,...
| |