I am new to VBA Access. I create "mytable" with 2 columns from existing main table by giving start date and end date.
Given below is mytable.
First column: dt( date hr:min:sec format )
Second column: string( the row elements could be true or false or none )
dt string
3/4/04 1:05:30 PM true
3/4/04 1:06:39 PM false
3/5/04 1:06:42 PM false
3/5/04 1:07:40 PM none
3/5/04 1:08:00 AM false
3/5/04 1:08:10 PM false
3/5/04 1:24:20 PM false
3/5/04 1:34:20 PM false
3/6/04 1:36:30 PM true
In the string column, the VBA code should look for "false". Look for a pattern where false is in continuous order like ( false2, false3 )
or (false5, false6, false7, false8) and increment the counter if the time difference (false3 - false2)>=180seconds or (false7-false5)>=180
or (false8 - false5)>= 180.
The output should give final value of counter and p = (counter/num)*100
In the above case, the output will be counter=3 because of (false6 - false5) ( false7 - false5 ) and (false8 - false5) and value of
p=(3/num)*100
num = datediff(false8 - false5)
Note: false2 means 2nd element in string column is false.If it would have been false5, false6 and false7 and then true8 then the counter should increment if (false7-false5)>=180seconds.
Output should be
Date counter p
3/4/04 0 0
3/5/04 3 (3/num)*100
3/6/04 0 0
If someone can give the VBA code that will be great.
Thanks in advance.
6 13928 MMcCarthy 14,534
Recognized Expert Moderator MVP OK try this. I'm not sure if I've caught all the logic but we'll see.
In the VB Editor window go to tools - references on the menu bar and make sure there is a Microsoft DAO library ticked.
In mytable change the field name string to myStr
Create a new table called myNewTable with fields; newDate, counter, p -
-
Function falseStats()
-
Dim db As Database
-
Dim rs As DAO.Recordset
-
Dim strSQL As String
-
Dim tempDate As Date
-
Dim NoOfDays As Integer
-
Dim tempValue As String
-
Dim NoOfSeconds As Integer
-
Dim counter As Integer
-
Dim result As Boolean
-
-
strSQL="SELECT * FROM mytable ORDER BY dt;"
-
-
Set db = CurrentDb
-
Set rs = db.OpenRecordset(strSQL)
-
rs.MoveFirst
-
Do until rs.EOF
-
result = True
-
tempDate = rs!dt,
-
tempValue = rs!myStr
-
NoOfSeconds=0
-
counter=0
-
If tempValue = "false" Then 'first record false
-
do until result=False 'repeat while false in value
-
rs.MoveNext
-
If rs!myStr = "false" Then 'next record is false
-
NoOfSeconds=DateDiff ('s', tempDate, rs!dt)
-
NoOfDays=DateDiff('d', tempDate, rs!dt)
-
If NoOfSecond >=180 Then
-
counter=counter+1
-
End If
-
Else
-
rs.MovePrev
-
DoCmd.RunSQL ("INSERT INTO myNewTable (newDate, Counter, p) " & _
-
"VALUES (Format(rs!dt,"Short Date"), counter, (counter/NoOfDays)*100);")
-
result=False
-
End If
-
loop
-
Else
-
DoCmd.RunSQL ("INSERT INTO myNewTable (newDate, Counter, p) VALUES (Format(rs!dt,"Short Date"), 0, 0);")
-
End If
-
rs.MoveNext
-
Loop
-
-
rs.Close
-
Set rs = Nothing
-
Set db = Nothing
-
-
End Function
-
-
OK try this. I'm not sure if I've caught all the logic but we'll see.
In the VB Editor window go to tools - references on the menu bar and make sure there is a Microsoft DAO library ticked.
In mytable change the field name string to myStr
Create a new table called myNewTable with fields; newDate, counter, p -
-
Function falseStats()
-
Dim db As Database
-
Dim rs As DAO.Recordset
-
Dim strSQL As String
-
Dim tempDate As Date
-
Dim NoOfDays As Integer
-
Dim tempValue As String
-
Dim NoOfSeconds As Integer
-
Dim counter As Integer
-
Dim result As Boolean
-
-
strSQL="SELECT * FROM mytable ORDER BY dt;"
-
-
Set db = CurrentDb
-
Set rs = db.OpenRecordset(strSQL)
-
rs.MoveFirst
-
Do until rs.EOF
-
result = True
-
tempDate = rs!dt,
-
tempValue = rs!myStr
-
NoOfSeconds=0
-
counter=0
-
If tempValue = "false" Then 'first record false
-
do until result=False 'repeat while false in value
-
rs.MoveNext
-
If rs!myStr = "false" Then 'next record is false
-
NoOfSeconds=DateDiff ('s', tempDate, rs!dt)
-
NoOfDays=DateDiff('d', tempDate, rs!dt)
-
If NoOfSecond >=180 Then
-
counter=counter+1
-
End If
-
Else
-
rs.MovePrev
-
DoCmd.RunSQL ("INSERT INTO myNewTable (newDate, Counter, p) " & _
-
"VALUES (Format(rs!dt,"Short Date"), counter, (counter/NoOfDays)*100);")
-
result=False
-
End If
-
loop
-
Else
-
DoCmd.RunSQL ("INSERT INTO myNewTable (newDate, Counter, p) VALUES (Format(rs!dt,"Short Date"), 0, 0);")
-
End If
-
rs.MoveNext
-
Loop
-
-
rs.Close
-
Set rs = Nothing
-
Set db = Nothing
-
-
End Function
-
-
Statements given below give errors.
tempDate = rs!dt, ( error because of comma )
result=False ( Error is False not defined)
NoOfSeconds=DateDiff ('s', tempDate, rs!dt) '( This is in red )
NoOfDays=DateDiff('d', tempDate, rs!dt) '( This is in red )
DoCmd.RunSQL ("INSERT INTO myNewTable (newDate, Counter, p) " & _
"VALUES (Format(rs!dt,"Short Date"), counter, (counter/NoOfDays)*100);") '( This is in red )
DoCmd.RunSQL ("INSERT INTO myNewTable (newDate, Counter, p) VALUES (Format(rs!dt,"Short Date"), 0, 0);") '( This is in red )
Please advise.
Thanks
NeoPa 32,557
Recognized Expert Moderator MVP
False not defined implies you have some library missing.
In the VBA debugger go to Tools / References... and see which are ticked
I thought False was a built-in constant so should be in the 'Visual Basic for Applications' library.
The others all seem to be ok (to me).
If they show as red in the debugger, try deleting them and re-entering them and note the error message. That might give a clue.
If it says the functions are not defined then that again is a library issue.
MMcCarthy 14,534
Recognized Expert Moderator MVP
Ok should all work now: -
-
Function falseStats()
-
Dim db As Database
-
Dim rs As DAO.Recordset
-
Dim strSQL As String
-
Dim tempDate As Date
-
Dim NoOfDays As Integer
-
Dim tempValue As String
-
Dim NoOfSeconds As Integer
-
Dim counter As Integer
-
Dim rslt As Boolean
-
-
strSQL = "SELECT * FROM mytable ORDER BY dt;"
-
-
Set db = CurrentDb
-
Set rs = db.OpenRecordset(strSQL)
-
rs.MoveFirst
-
Do Until rs.EOF
-
rslt = True
-
tempDate = rs!dt
-
tempValue = rs!myStr
-
NoOfSeconds = 0
-
counter = 0
-
If tempValue = "false" Then 'first record false
-
Do Until rslt = False 'repeat while false in value
-
rs.MoveNext
-
If rs!myStr = "false" Then 'next record is false
-
NoOfSeconds = DateDiff("s", tempDate, rs!dt)
-
NoOfDays = DateDiff("d", tempDate, rs!dt)
-
If NoOfSeconds >= 180 Then
-
counter = counter + 1
-
End If
-
Else
-
rs.MovePrevious
-
DoCmd.RunSQL ("INSERT INTO myNewTable (newDate, Counter, p) " & _
-
"VALUES (Format(rs!dt,'Short Date'), counter, (counter/NoOfDays)*100);")
-
rslt = False
-
End If
-
Loop
-
Else
-
DoCmd.RunSQL ("INSERT INTO myNewTable (newDate, Counter, p) VALUES (Format(rs!dt,'Short Date'), 0, 0);")
-
End If
-
rs.MoveNext
-
Loop
-
-
rs.Close
-
Set rs = Nothing
-
Set db = Nothing
-
-
End Function
-
-
Ok should all work now: -
-
Function falseStats()
-
Dim db As Database
-
Dim rs As DAO.Recordset
-
Dim strSQL As String
-
Dim tempDate As Date
-
Dim NoOfDays As Integer
-
Dim tempValue As String
-
Dim NoOfSeconds As Integer
-
Dim counter As Integer
-
Dim rslt As Boolean
-
-
strSQL = "SELECT * FROM mytable ORDER BY dt;"
-
-
Set db = CurrentDb
-
Set rs = db.OpenRecordset(strSQL)
-
rs.MoveFirst
-
Do Until rs.EOF
-
rslt = True
-
tempDate = rs!dt
-
tempValue = rs!myStr
-
NoOfSeconds = 0
-
counter = 0
-
If tempValue = "false" Then 'first record false
-
Do Until rslt = False 'repeat while false in value
-
rs.MoveNext
-
If rs!myStr = "false" Then 'next record is false
-
NoOfSeconds = DateDiff("s", tempDate, rs!dt)
-
NoOfDays = DateDiff("d", tempDate, rs!dt)
-
If NoOfSeconds >= 180 Then
-
counter = counter + 1
-
End If
-
Else
-
rs.MovePrevious
-
DoCmd.RunSQL ("INSERT INTO myNewTable (newDate, Counter, p) " & _
-
"VALUES (Format(rs!dt,'Short Date'), counter, (counter/NoOfDays)*100);")
-
rslt = False
-
End If
-
Loop
-
Else
-
DoCmd.RunSQL ("INSERT INTO myNewTable (newDate, Counter, p) VALUES (Format(rs!dt,'Short Date'), 0, 0);")
-
End If
-
rs.MoveNext
-
Loop
-
-
rs.Close
-
Set rs = Nothing
-
Set db = Nothing
-
-
End Function
-
-
I apologize for the late reply. But when I run the code, it asks for parameters( I dont want that way ). Please advise.
NeoPa 32,557
Recognized Expert Moderator MVP
Which parameters does it ask for?
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Jay |
last post by:
I previously posted this question under Visual Basic
newsgroup, but was advised to re-post here.
I'm hoping someone can help me solve an issue I'm having
with VB.Net and Access 2000.
Here's...
|
by: mark |
last post by:
In Access 2000 and 2002, I have created an import specification to import the
fixed-width recordset below into an existing table. I am having strange
problems with the import of the date and time...
|
by: bhbgroup |
last post by:
I have a query on one large table. I only add one condition, i.e. a
date (the SQL reads like 'where date > parameterdate'.
This query is rather quick if 'parameterdate' is either explicitly...
|
by: cflarida |
last post by:
I have a simple form that will gather it's information from a card
reader. The form has 3 fields SSN which is gathered from the card
swipe. Can I make access automaticly hit enter when it hits...
|
by: Henning M |
last post by:
Hi all,
I having some problems with Access and selecting records between dates..
When I try this in access, it works fine!!
"Select * from Bilag Where Mdates Between #1/1/2006# And...
| |
by: Jim in Arizona |
last post by:
I have a gridview that's being populated from an access db query. The
problem I'm having is that the date/time fields in access that are
populating the gridview are showing both date and time, when...
|
by: Sheldon |
last post by:
I have the following query expression -
Like Format((!!)) & "/*/" & (!
!)
which would translate to e.g. 04/*/2007 if someone is running a report
for last month. The above expression is part of a...
|
by: SilentThunderer |
last post by:
Hey folks,
Let me start out by letting you know what I'm working with. I'm building an application in VB 2005 that is basically a userform that employees can use to "Clock in". The form...
|
by: MMcCarthy |
last post by:
Access has a number of built-in functions which can be generally used in queries or VBA code. Some of the more common ones are:
Note: anything in square brackets is optional
Date Functions
...
|
by: Mark |
last post by:
Currently using MS Access 2000 and SQL Server Express. Using the
current DAO OpenRecordset code listed below, however I keep getting
the error message....
3254: ODBC --Cannot lock all records
...
|
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: 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: 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: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |