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 :)
10 3412 NeoPa 32,557
Recognized Expert Moderator MVP
WHERE clause should be in form :- - WHERE (({Cold stuff}) OR ({Flu stuff}))
Where {Cold stuff} is :- - ([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
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 ?
Sorry for the messy alignment~hope this is better..nt sure y the alignment keeps goin off. -
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 /
-
-
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 /
-
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 :- - WHERE ((([cold]>0) AND ([cold ever]=1) AND ([cold date]='/')) OR _
-
(([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.
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.
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 -
-
Dim dbs As DAO.Database
-
Dim strSQL As String
-
Dim qdf As DAO.QueryDef
-
-
Set dbs = OpenDatabase("path and name")
-
strSQL = "SELECT Statement"
-
Set qry = dbs.CreateQueryDefs("MyQuery", strSQL)
-
-
' this should create a query in the nominated database called MyQuery
-
-
Set qdf = Nothing
-
rst.Close
-
Set rst = Nothing
-
Set dbs = Nothing
-
-
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) - -id-no cold cold ever colddate flu fluever fludate
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
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]
Thanks again mmccarthy !Yap, as what you've suggested, put sth else beside NULL.My problem is solved!!:)
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' :(.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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: 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: 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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |