473,545 Members | 2,041 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 3419
NeoPa
32,563 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,563 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,563 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\ne w.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\ne w.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

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

Similar topics

1
6341
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 database using unicode compression field (for ancient language). I would like to export this table into MySQL o Postgres, but it's
5
11010
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 setting stDataSource and stFileName to the desired values. Most of the time it works, but occasionally, typically as code changes are being made to...
6
13108
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 spreadsheet. I'm having trouble with my code at the point at which it hits ".ReadRecords" -- the module just runs and runs without generating anything. I've...
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 was going to over-write the table from database B, I got an error said "You can delete table "xyz"; it is participating in one or more...
4
2268
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 to a SmartList to go db. The SMtG Access sync will not allow you to sync to a query based on more than one table. And if I sync all the records...
11
6073
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 application controls allocation of revisions to aircraft maintenance manuals for an airline type operation. In the application there is a form loaded...
2
5515
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 Form" in order to search for records based on this form, I would like to be able to enter a value in this "days" control so that I can filter records on...
1
9757
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 having I'd be most appreciative. The database is already constructed, I'm just wanting to export the data to an excel file. In short, I'm hoping...
9
7685
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 is 8 characters long. This field needs to be exported as pic(15) padded in the front with 0's (zeros). The next field an ID name that is 15...
0
7473
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...
0
7408
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...
0
7815
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7433
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...
0
7763
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...
0
5976
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...
0
4949
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...
1
1020
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
712
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...

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.