473,509 Members | 3,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to filter and export data into another database using Access Module?

5 New Member
Hi,
I have a Table containing these fields: id-no, cold, cold ever, cold date,flu,flu ever and flu date. (The properties of id-no, cold,cold ever,flu,flu ever are set as Numbers.)
What i want to do is to select and export the data into another database that has satified the criteria using Access Module.Is it possible?I've tried using the following code but it doesnt seem to work accordin to what i want.

Public Sub MakeTable_new()

DoCmd.RunSQL ("SELECT [table1].[id-no], [table1].[cold], [table1].[cold ever], [table1].[cold date], [table1].[flu], [table1].[flu ever], [table].[flu date]" & _
"INTO [newtable] IN 'C:\ME\new.mdb'" & _
"FROM [table1]" & _
"Where (([table1].[cold])>0 AND ([table1].[cold ever])=1 AND ([table1].[cold date])='/' Or ([table1].[flu])>0 AND ([table1].[flu ever])=1 AND ([table1].[flu date])='/')" & _
"ORDER BY [table1].[id-no];")

End Sub

When I run the above the code, the data of fields "cold, cold ever and cold date" are selected and exported correctly according to the criteria however, the data of fields "flu,flu ever and flu date" wasnt. I hope someone could point out the mistakes in my coding or enhance it so it works properly or provide alternatives tt will perform the same task if possible.
Thanks in advance :)
Oct 26 '06 #1
10 3412
NeoPa
32,557 Recognized Expert Moderator MVP
WHERE clause should be in form :-
Expand|Select|Wrap|Line Numbers
  1. WHERE (({Cold stuff}) OR ({Flu stuff}))
Where {Cold stuff} is :-
Expand|Select|Wrap|Line Numbers
  1. ([cold]>0) AND ([cold ever]=1) AND ([cold date]='/')
I think your parentheses are the problem here.
I'm assuming from your code that [cold date] = '/' has a meaning to you - I'm just copying that bit from your code
Oct 26 '06 #2
coffeesin
5 New Member
NeoPa thanks for your reply if i change the parentheses to { } as what you suggested i would get debug error. Also, ive modified my code abit but still it failed to work exactly what i want.

Public Sub MakeTable_ced()
DoCmd.RunSQL ("SELECT [Table1].[id-no], [Table1].[cold], [Table1].[cold ever], [Table1].[cold date], [Table1].[flu], [Table1].[flu ever], [Table1].[flu date]" & _
"INTO [newtable] IN 'C:\ME\new.mdb'" & _
"FROM [Table1]" & _
"WHERE(([Table1].[cold])>0 AND ([Table1].[cold ever])=1 AND ([Table1].[cold date])='/' Or ([Table1].[cold])>0 AND ([Table1].[cold ever])=0 AND ([Table1].[cold date])='/' Or([Table1].[flu])>0 AND ([Table1].[flu ever])=1 AND([Table1].[flu date])='/' Or([Table1].[flu])>0 AND ([Table1].[flu ever])=0 AND([Table1].[flu date])='/')" &
"ORDER BY [Table1].[id-no];")
End Sub

The following is the data in the 'Table1':
id-no cold cold ever cold date flu flu ever flu date
1001 1 0 15/08/2006 1 0 13/08/2004
1005 2 1 14/09/2004 2 1 13/08/2004
1003 2 2 14/11/2005 0 1 14/08/2006
1008 1 1 14/07/2004 1 0 /
1004 1 1 / 1 1 13/07/2004
1009 2 0 / 1 1 /

When i run the code, the filtered data appear in 'newtable' in new database is as follow:
id-no cold cold ever cold date flu flu ever flu date
1004 1 1 / 1 1 13/07/2004
1008 1 1 14/07/2004 1 0 /
1009 2 0 / 1 1 /

the data of id no 1004 has satisfied the criteria are displayed (i.e data of cold, cold ever and cold data fields ) however the data that does not satisfied the criteria are displayed as well (ie data of flu,flu ever and flu date).Similarly,this happens to id no 1008. So is there a possible way to display only the data tt satisfy the criteria ?
Oct 26 '06 #3
coffeesin
5 New Member
Sorry for the messy alignment~hope this is better..nt sure y the alignment keeps goin off.
Expand|Select|Wrap|Line Numbers
  1. id-no    cold  cold ever  cold date    flu     flu ever     flu date
  2. 1001      1         0      15/08/2006    1    0     13/08/2004
  3. 1005      2         1      14/09/2004    2    1     13/08/2004
  4. 1003      2         2      14/11/2005    0    1     14/08/2006
  5. 1008      1         1      14/07/2004    1    0     /
  6. 1004      1         1         /                1    1     13/07/2004
  7. 1009      2      0         /                1    1     /
  8.  

Expand|Select|Wrap|Line Numbers
  1. id-no    cold   cold ever   cold date    flu    flu ever    flu date
  2. 1004    1    1    /    1    1    13/07/2004
  3. 1008    1    1      14/07/2004    1    0    /
  4. 1009    2    0    /    1    1    /
  5.  
Oct 26 '06 #4
NeoPa
32,557 Recognized Expert Moderator MVP
You're absolutely right about the {} characters CoffeSin but I didn't mean it quite like that.
In the top Code box I referred to {Cold stuff} & {Flu stuff}
In the bottom box I illustrated with an example, what {Cold stuff} might be.

In long hand then, it would be :-
Expand|Select|Wrap|Line Numbers
  1. WHERE ((([cold]>0) AND ([cold ever]=1) AND ([cold date]='/')) OR _
  2. (([flu]>0) AND ([flu ever]=1) AND ([flu date]='/')))
I just thought that would be harder to read and understand.

I hope this is clearer.
Oct 26 '06 #5
NeoPa
32,557 Recognized Expert Moderator MVP
Sorry for the messy alignment~hope this is better..nt sure y the alignment keeps goin off.
That will be because the forum management software parses all the entries before loading it into its database.
Typically it strips Tabs and converts multiple spaces into just the one.
Your idea of using the [code] tags works well though - a point worth noting for all those with similar issues.
Oct 26 '06 #6
MMcCarthy
14,534 Recognized Expert Moderator MVP
I'm pretty sure you can't run this type of query using runsql its designed for action queries like insert, delete and update.

you will have to create a querydef variable

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim dbs As DAO.Database
  3. Dim strSQL As String
  4. Dim qdf As DAO.QueryDef
  5.  
  6.     Set dbs = OpenDatabase("path and name")
  7.     strSQL = "SELECT Statement"
  8.     Set qry = dbs.CreateQueryDefs("MyQuery", strSQL)
  9.  
  10.     ' this should create a query in the nominated database called MyQuery
  11.  
  12.     Set qdf = Nothing
  13.     rst.Close
  14.     Set rst = Nothing
  15.     Set dbs = Nothing
  16.  
  17.  
Oct 27 '06 #7
coffeesin
5 New Member
Hi mmccarthy, thanks for your solution and i ve tried but i kept receiving debug error and im still new to access module so im v not familiar with DAO objects. Thus, i tried to modify my code abit, it did display the records that matched to the criteria i set and blanked those that do not. However, another problem arises.
When the records of the fields:flu,flu ever and flu date are appended to the 'newtable', the properties of these fields were set to 'Binary' and thus the records were not properly displayed. (Pls see the following)

Expand|Select|Wrap|Line Numbers
  1. -id-no cold cold ever colddate   flu  fluever  fludate
Expand|Select|Wrap|Line Numbers
  1. 100     1     0     /                                              
Expand|Select|Wrap|Line Numbers
  1. 101     2     1     /                                              
Expand|Select|Wrap|Line Numbers
  1. 105                                                    /        
I've tried to chnge the properties to the appropriate data type but the records of flu fields would be deleted as well. The following is the modified code:

Public Sub MakeTable_ced()

DoCmd.RunSQL ("SELECT [Table1].[id-no], [Table1].[cold], [Table1].[cold ever], [Table1].[cold date], Null As [flu], Null As [flu ever], Null As [flu date] " & _
"INTO [newtable] IN 'C:\Documents and Settings\guo\My Documents\ME\new.mdb'" & _
"FROM [Table1] " & _
"WHERE(((([Table1].[cold])>0) AND (([Table1].[cold ever])=1) AND (([Table1].[cold date])='/')) Or ((([Table1].[cold])>0) AND (([Table1].[cold ever])=0) AND (([Table1].[cold date])='/')))" & _
"ORDER BY [Table1].[id-no];")

DoCmd.RunSQL ("INSERT INTO newtable( [id-no], flu, [flu ever], [flu date] ) IN 'C:\Documents and Settings\guo\My Documents\ME\new.mdb'" & _
"SELECT [Table1].[id-no], [Table1].[flu], [Table1].[flu ever], [Table1].[flu date]" & _
"FROM [Table1]" & _
"WHERE (((([Table1].[flu])>0) AND (([Table1].[flu ever])=1) AND (([Table1].[flu date])='/')) OR ((([Table1].[flu])>0) AND (([Table1].[flu ever])=0) AND (([Table1].[flu date])='/')))" & _
"ORDER BY [Table1].[id-no];")

End Sub
Oct 28 '06 #8
MMcCarthy
14,534 Recognized Expert Moderator MVP
Don't use null use something else like:

SELECT [Table1].[id-no], [Table1].[cold], [Table1].[cold ever], [Table1].[cold date], 0 As [flu], 0 As [flu ever], "" As [flu date]
Oct 28 '06 #9
coffeesin
5 New Member
Thanks again mmccarthy !Yap, as what you've suggested, put sth else beside NULL.My problem is solved!!:)
Oct 29 '06 #10
NeoPa
32,557 Recognized Expert Moderator MVP
There should really be a way of specifying in Access SQL to add a field whose :-
Name = [asdfg]
Value set to Null
Type = Date / String / Integer etc.
But if there is, Microsoft have a good way of hiding it in their help file.

Their helpful comments are
When you create the table, the fields in the new table inherit the data type and field size of each field in the query's underlying tables, but no other field or table properties are transferred.
So constants or literals are 'best-guessed' :(.
Oct 29 '06 #11

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

Similar topics

1
6335
by: JJ | last post by:
Hi, usually, I'm not using MS servers, but I have a big problem with a Access table. I should create a web application for a Historical Dipartment. They have create a populated a Access...
5
11006
by: Tim Eliot | last post by:
Just wondering if anyone has hit the following issue and how you might have sorted it out. I am using the command: DoCmd.TransferText acExportMerge, , stDataSource, stFileName, True after...
6
13099
by: Robin Cushman | last post by:
Hi all, I need some help -- I'm working with an A2K database, using DAO, and am trying to read records into a Crystal Report and then export it to a folder on our network as an Excel...
3
2604
by: eddie wang | last post by:
Hello, I am trying to export all the data from one Access database to another Access database. I selected one of my five tables, then clicked "File"-->"Export". Because my table from database A...
4
2267
by: Brian | last post by:
I hope this will make sense. I'm trying to filter the records in a table based on records in a 2nd table. The trick is, I can't use a query. I'm trying to filter down the number of records going...
11
6068
by: Bob | last post by:
I am in the process of upgrading an Access database to SQL Server (and climbing that learning curve!). The wizard happily upgraded all the tables and I can link to them OK using ODBC. The...
2
5507
by: jcf378 | last post by:
hi all. I have a form which contains a calculated control ("days") that outputs the # of days between two dates (DateDiff command between the fields and ). However, when I click "Filter by...
1
9755
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm...
9
7675
by: NEWSGROUPS | last post by:
I have data in a table in an Access 2000 database that needs to be exported to a formatted text file. For instance, the first field is an account number that is formatted in the table as text and...
0
7234
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
7344
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,...
1
7069
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...
1
5060
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...
0
4730
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...
0
3216
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...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
441
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...

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.