473,508 Members | 2,265 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VBA Access date time

4 New Member
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.
Oct 24 '06 #1
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. Function falseStats()
  3. Dim db As Database
  4. Dim rs As DAO.Recordset
  5. Dim strSQL As String
  6. Dim tempDate As Date
  7. Dim NoOfDays As Integer
  8. Dim tempValue As String
  9. Dim NoOfSeconds As Integer
  10. Dim counter As Integer
  11. Dim result As Boolean
  12.  
  13.  strSQL="SELECT * FROM mytable ORDER BY dt;"
  14.  
  15.  Set db = CurrentDb
  16.  Set rs = db.OpenRecordset(strSQL)
  17.  rs.MoveFirst
  18.  Do until rs.EOF
  19.   result = True
  20.   tempDate = rs!dt,
  21.   tempValue = rs!myStr
  22.   NoOfSeconds=0
  23.   counter=0
  24.   If tempValue = "false" Then 'first record false
  25.    do until result=False   'repeat while false in value
  26.     rs.MoveNext
  27.     If rs!myStr = "false" Then 'next record is false
  28.      NoOfSeconds=DateDiff ('s', tempDate, rs!dt)
  29.      NoOfDays=DateDiff('d', tempDate, rs!dt)
  30.      If NoOfSecond >=180 Then
  31.       counter=counter+1
  32.      End If
  33.     Else
  34.      rs.MovePrev
  35.      DoCmd.RunSQL ("INSERT INTO myNewTable (newDate, Counter, p) " & _
  36.                    "VALUES (Format(rs!dt,"Short Date"), counter, (counter/NoOfDays)*100);")
  37.      result=False
  38.     End If
  39.    loop
  40.   Else
  41.    DoCmd.RunSQL ("INSERT INTO myNewTable (newDate, Counter, p) VALUES (Format(rs!dt,"Short Date"), 0, 0);")
  42.   End If
  43.   rs.MoveNext
  44.  Loop
  45.  
  46.  rs.Close
  47.  Set rs = Nothing
  48.  Set db = Nothing
  49.  
  50. End Function
  51.  
  52.  
Oct 25 '06 #2
gre1unix
4 New Member
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. Function falseStats()
  3. Dim db As Database
  4. Dim rs As DAO.Recordset
  5. Dim strSQL As String
  6. Dim tempDate As Date
  7. Dim NoOfDays As Integer
  8. Dim tempValue As String
  9. Dim NoOfSeconds As Integer
  10. Dim counter As Integer
  11. Dim result As Boolean
  12.  
  13.  strSQL="SELECT * FROM mytable ORDER BY dt;"
  14.  
  15.  Set db = CurrentDb
  16.  Set rs = db.OpenRecordset(strSQL)
  17.  rs.MoveFirst
  18.  Do until rs.EOF
  19.   result = True
  20.   tempDate = rs!dt,
  21.   tempValue = rs!myStr
  22.   NoOfSeconds=0
  23.   counter=0
  24.   If tempValue = "false" Then 'first record false
  25.    do until result=False   'repeat while false in value
  26.     rs.MoveNext
  27.     If rs!myStr = "false" Then 'next record is false
  28.      NoOfSeconds=DateDiff ('s', tempDate, rs!dt)
  29.      NoOfDays=DateDiff('d', tempDate, rs!dt)
  30.      If NoOfSecond >=180 Then
  31.       counter=counter+1
  32.      End If
  33.     Else
  34.      rs.MovePrev
  35.      DoCmd.RunSQL ("INSERT INTO myNewTable (newDate, Counter, p) " & _
  36.                    "VALUES (Format(rs!dt,"Short Date"), counter, (counter/NoOfDays)*100);")
  37.      result=False
  38.     End If
  39.    loop
  40.   Else
  41.    DoCmd.RunSQL ("INSERT INTO myNewTable (newDate, Counter, p) VALUES (Format(rs!dt,"Short Date"), 0, 0);")
  42.   End If
  43.   rs.MoveNext
  44.  Loop
  45.  
  46.  rs.Close
  47.  Set rs = Nothing
  48.  Set db = Nothing
  49.  
  50. End Function
  51.  
  52.  

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
Oct 25 '06 #3
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.
Oct 25 '06 #4
MMcCarthy
14,534 Recognized Expert Moderator MVP
Ok should all work now:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Function falseStats()
  3. Dim db As Database
  4. Dim rs As DAO.Recordset
  5. Dim strSQL As String
  6. Dim tempDate As Date
  7. Dim NoOfDays As Integer
  8. Dim tempValue As String
  9. Dim NoOfSeconds As Integer
  10. Dim counter As Integer
  11. Dim rslt As Boolean
  12.  
  13.     strSQL = "SELECT * FROM mytable ORDER BY dt;"
  14.  
  15.     Set db = CurrentDb
  16.     Set rs = db.OpenRecordset(strSQL)
  17.     rs.MoveFirst
  18.     Do Until rs.EOF
  19.         rslt = True
  20.         tempDate = rs!dt
  21.         tempValue = rs!myStr
  22.         NoOfSeconds = 0
  23.         counter = 0
  24.         If tempValue = "false" Then 'first record false
  25.         Do Until rslt = False 'repeat while false in value
  26.             rs.MoveNext
  27.             If rs!myStr = "false" Then 'next record is false
  28.                 NoOfSeconds = DateDiff("s", tempDate, rs!dt)
  29.                 NoOfDays = DateDiff("d", tempDate, rs!dt)
  30.                 If NoOfSeconds >= 180 Then
  31.                     counter = counter + 1
  32.                 End If
  33.             Else
  34.                 rs.MovePrevious
  35.                 DoCmd.RunSQL ("INSERT INTO myNewTable (newDate, Counter, p) " & _
  36.                           "VALUES (Format(rs!dt,'Short Date'), counter, (counter/NoOfDays)*100);")
  37.                 rslt = False
  38.             End If
  39.         Loop
  40.     Else
  41.         DoCmd.RunSQL ("INSERT INTO myNewTable (newDate, Counter, p) VALUES (Format(rs!dt,'Short Date'), 0, 0);")
  42.     End If
  43.     rs.MoveNext
  44.     Loop
  45.  
  46.  rs.Close
  47.  Set rs = Nothing
  48.  Set db = Nothing
  49.  
  50. End Function
  51.  
  52.  
Oct 25 '06 #5
gre1unix
4 New Member
Ok should all work now:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Function falseStats()
  3. Dim db As Database
  4. Dim rs As DAO.Recordset
  5. Dim strSQL As String
  6. Dim tempDate As Date
  7. Dim NoOfDays As Integer
  8. Dim tempValue As String
  9. Dim NoOfSeconds As Integer
  10. Dim counter As Integer
  11. Dim rslt As Boolean
  12.  
  13.     strSQL = "SELECT * FROM mytable ORDER BY dt;"
  14.  
  15.     Set db = CurrentDb
  16.     Set rs = db.OpenRecordset(strSQL)
  17.     rs.MoveFirst
  18.     Do Until rs.EOF
  19.         rslt = True
  20.         tempDate = rs!dt
  21.         tempValue = rs!myStr
  22.         NoOfSeconds = 0
  23.         counter = 0
  24.         If tempValue = "false" Then 'first record false
  25.         Do Until rslt = False 'repeat while false in value
  26.             rs.MoveNext
  27.             If rs!myStr = "false" Then 'next record is false
  28.                 NoOfSeconds = DateDiff("s", tempDate, rs!dt)
  29.                 NoOfDays = DateDiff("d", tempDate, rs!dt)
  30.                 If NoOfSeconds >= 180 Then
  31.                     counter = counter + 1
  32.                 End If
  33.             Else
  34.                 rs.MovePrevious
  35.                 DoCmd.RunSQL ("INSERT INTO myNewTable (newDate, Counter, p) " & _
  36.                           "VALUES (Format(rs!dt,'Short Date'), counter, (counter/NoOfDays)*100);")
  37.                 rslt = False
  38.             End If
  39.         Loop
  40.     Else
  41.         DoCmd.RunSQL ("INSERT INTO myNewTable (newDate, Counter, p) VALUES (Format(rs!dt,'Short Date'), 0, 0);")
  42.     End If
  43.     rs.MoveNext
  44.     Loop
  45.  
  46.  rs.Close
  47.  Set rs = Nothing
  48.  Set db = Nothing
  49.  
  50. End Function
  51.  
  52.  
I apologize for the late reply. But when I run the code, it asks for parameters( I dont want that way ). Please advise.
Oct 31 '06 #6
NeoPa
32,557 Recognized Expert Moderator MVP
Which parameters does it ask for?
Oct 31 '06 #7

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

Similar topics

3
7812
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...
1
8160
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...
4
2037
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...
3
1406
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...
5
2541
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...
3
3274
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...
3
3840
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...
4
6791
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...
1
19384
MMcCarthy
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 ...
6
6200
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 ...
0
7231
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
7336
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,...
0
7401
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...
1
7063
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
5059
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
3211
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
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
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 ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.