By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

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

P: 5
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
Share this Question
Share on Google+
10 Replies


NeoPa
Expert Mod 15k+
P: 31,494
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

P: 5
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

P: 5
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
Expert Mod 15k+
P: 31,494
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
Expert Mod 15k+
P: 31,494
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
Expert Mod 10K+
P: 14,534
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

P: 5
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
Expert Mod 10K+
P: 14,534
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

P: 5
Thanks again mmccarthy !Yap, as what you've suggested, put sth else beside NULL.My problem is solved!!:)
Oct 29 '06 #10

NeoPa
Expert Mod 15k+
P: 31,494
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

Post your reply

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