473,511 Members | 16,110 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Looping A Specified Number Of Times

Hi,

hopeing someone can help me here...

I have a deductions table and form and want to be able to add many deductions
to the table by having only a few fields on the form

form fields: deduction ID, start date, end date and amount

what i want to be able to do is keep adding a deduction each 7 days from the
start date to the end date. ie only loop the number of times (01/01/01 to
14/01/01 would add 2 records)

hope i made some sense...

thanks
Lee-Anne
Nov 13 '05 #1
2 1990
"ozgirl via AccessMonster.com" <u14396@uwe> wrote in message
news:5514041f53b76@uwe...
Hi,

hopeing someone can help me here...

I have a deductions table and form and want to be able to add many
deductions
to the table by having only a few fields on the form

form fields: deduction ID, start date, end date and amount

what i want to be able to do is keep adding a deduction each 7 days from
the
start date to the end date. ie only loop the number of times (01/01/01 to
14/01/01 would add 2 records)

hope i made some sense...

thanks
Lee-Anne



When you say "fields on the form" it is not clear if this is a bound form or
whether you simply mean textboxes on the form. You also don't say whether a
single value for [deduction id] goes in for each record as the same value or
whether this is some sort of autonumber for the table which we don't have to
worry about setting a value for.
So I'm assuming your table is called [My Table] and each time we need to put
in values for an incrementing [My Date] and a static [My Amount]. Your form
has textboxes called txtStartDate, txtEndDate and txtAmount and a button
cmdUpdate which runs the code.
The complication with this code is that because you want multiple records to
be added you must make sure you wrap it all up in a transaction, otherwise
you may add only, say 3 of 5 records if some error occurred. Better that
you add either zero records and report the error or all 5 and report
success.

Private Sub cmdUpdate_Click()

On Error GoTo Err_Handler

Dim dteStart As Date
Dim dteEnd As Date
Dim curAmount As Currency
Dim strSQL As String
Dim wks As DAO.Workspace
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim lngCount As Long

If IsNull(Me.txtStartDate) Then
MsgBox "Enter a start date", vbInformation
Me.txtStartDate.SetFocus
Exit Sub
Else
dteStart = CDate(Me!txtStartDate)
End If

If IsNull(Me.txtEndDate) Then
MsgBox "Enter an end date", vbInformation
Me.txtEndDate.SetFocus
Exit Sub
Else
dteEnd = CDate(Me!txtEndDate)
End If

If IsNull(Me.txtAmount) Then
MsgBox "Enter an amount", vbInformation
Me.txtAmount.SetFocus
Exit Sub
Else
curAmount = CCur(Me!txtAmount)
End If

strSQL = "SELECT * FROM [My Table]"

Set wks = DBEngine.Workspaces(0)

Set dbs = CurrentDb()

Set rst = dbs.OpenRecordset(strSQL, dbOpenDynaset, dbAppendOnly)

wks.BeginTrans

While dteEnd >= dteStart
rst.AddNew
rst.Fields("My Date") = dteStart
rst.Fields("My Amount") = curAmount
rst.Update
lngCount = lngCount + 1
dteStart = DateAdd("d", 7, dteStart)
Wend

wks.CommitTrans

MsgBox CStr(lngCount) & " records(s) added", vbInformation

Exit_Handler:
On Error Resume Next
rst.Close
Set rst = Nothing
Set dbs = Nothing
Set wks = Nothing
Exit Sub

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub


Nov 13 '05 #2
many thanks Brian..

much appreciated...

Lee-Anne

Brian Wilson wrote:
Hi,

[quoted text clipped - 15 lines]
thanks
Lee-Anne


When you say "fields on the form" it is not clear if this is a bound form or
whether you simply mean textboxes on the form. You also don't say whether a
single value for [deduction id] goes in for each record as the same value or
whether this is some sort of autonumber for the table which we don't have to
worry about setting a value for.
So I'm assuming your table is called [My Table] and each time we need to put
in values for an incrementing [My Date] and a static [My Amount]. Your form
has textboxes called txtStartDate, txtEndDate and txtAmount and a button
cmdUpdate which runs the code.
The complication with this code is that because you want multiple records to
be added you must make sure you wrap it all up in a transaction, otherwise
you may add only, say 3 of 5 records if some error occurred. Better that
you add either zero records and report the error or all 5 and report
success.

Private Sub cmdUpdate_Click()

On Error GoTo Err_Handler

Dim dteStart As Date
Dim dteEnd As Date
Dim curAmount As Currency
Dim strSQL As String
Dim wks As DAO.Workspace
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim lngCount As Long

If IsNull(Me.txtStartDate) Then
MsgBox "Enter a start date", vbInformation
Me.txtStartDate.SetFocus
Exit Sub
Else
dteStart = CDate(Me!txtStartDate)
End If

If IsNull(Me.txtEndDate) Then
MsgBox "Enter an end date", vbInformation
Me.txtEndDate.SetFocus
Exit Sub
Else
dteEnd = CDate(Me!txtEndDate)
End If

If IsNull(Me.txtAmount) Then
MsgBox "Enter an amount", vbInformation
Me.txtAmount.SetFocus
Exit Sub
Else
curAmount = CCur(Me!txtAmount)
End If

strSQL = "SELECT * FROM [My Table]"

Set wks = DBEngine.Workspaces(0)

Set dbs = CurrentDb()

Set rst = dbs.OpenRecordset(strSQL, dbOpenDynaset, dbAppendOnly)

wks.BeginTrans

While dteEnd >= dteStart
rst.AddNew
rst.Fields("My Date") = dteStart
rst.Fields("My Amount") = curAmount
rst.Update
lngCount = lngCount + 1
dteStart = DateAdd("d", 7, dteStart)
Wend

wks.CommitTrans

MsgBox CStr(lngCount) & " records(s) added", vbInformation

Exit_Handler:
On Error Resume Next
rst.Close
Set rst = Nothing
Set dbs = Nothing
Set wks = Nothing
Exit Sub

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub

--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200509/1
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4039
by: kaptain kernel | last post by:
i've got a while loop thats iterating through a text file and pumping the contents into a database. the file is quite large (over 150mb). the looping causes my CPU load to race up to 100 per...
4
4875
by: David | last post by:
Hello. I am looking for advice on what is "best practice" regarding looping through a form to check its checkboxes and associated data fields. Here is what I am trying to do (Here is the page...
27
2583
by: Mike P | last post by:
I will be passing my function a two dimensional array of varying length. Within that array is one data point, and the number of times it should loop through. So, for example, I might pass this...
4
1662
by: Paul M | last post by:
Larry, i've got an xml file which has multiple cities as the top level item. When i read it in using XMLReader, and define the variables, as below, it finds the correct number of cities in the...
5
1430
by: Karen | last post by:
Most my experience has been with MySQL, so I write queries in terms of SQL statements. I am using access right now as it is on the machine I have. I am using the SQL window as I couldn't figure out...
5
1782
by: Bruce Lawrence | last post by:
I'm running Access 97 and my modules are looping if someone puts an invalid value in. The setup: 3 macros - get_clock_num, verify_clocknum, append_to_history 3 functions. each in their own...
7
3340
by: No bother | last post by:
I have a table which has, among other fields, a date field. I want to get a count of records where certain criteria are met for, say, three days in a row. For example: NumWidgets Date...
20
2799
by: Ifoel | last post by:
Hi all, Sorry im beginer in vb. I want making programm looping character or number. Just say i have numbers from 100 to 10000. just sample: Private Sub Timer1_Timer() if check1.value= 1...
3
1892
by: assgar | last post by:
Hi I am having problem with my loping. I don't know if I have chosen the correct approach. GOAL: I need to insert into a table event types for a specific date range. The calendar the event...
0
7242
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
7353
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,...
1
7075
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...
0
7508
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...
1
5063
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
4737
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...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
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
781
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.