This topic is locked

Bulk Insert

8/16/2007 5:32:26 PM
ASPRunnerPro General questions
D
David J S author

How can I insert many/multiple records in a table based on a range selecton (i.e. Date or date range)?
For example I have meetings that occur at location "A" on Tuesdays at 5:00 PM and Thursdays at 3:30 PM, at location "B" Fridays at 5:00 PM, etc. The data is derived from a meeting table that gives a day of the week and time of meeting. I want to add records to a table that keeps track of meeting "notes". I want this meeting to exist before the notes are to be entered so that the user can select the appropriate record to add meeting notes. The result would be to add "N" weeks of records to the db.
So, the basic question is how to enter multiple records to a table based on a date range entered by a user joined with a couple fields of data from a supporting/parent table.
I hope there is an easy fix for this.
Dave
ASPRunnerPro 5.0 (Build 262)

SQL Server 2000

Sergey Kornilov admin 8/17/2007

Dave,
here is the example of what you can try.
You can modify Delete selected label to say Add/Insert selected.

After that you can use BeforeDelete event to create a copy of each selected record in another table.

D
David J S author 9/21/2007

Sergey,
The suggestion that you made worked great! Thanks. It took me a while to figure out the syntax for capturing the right "request" data but once I figured that out (trial and error) it worked fine. Is there something like a cheat sheet for data that is on the calling pages? and how to capture it? I have included the code from my page that shows how it was done in case someone else has a similar need.
Thanks,
Dave
[codebox]Function BeforeDelete(where)
dim ind, dBeginDate, dEndDate
ind = clng(Request("mdelete[]"))

dBeginDate = Replace(Request("mdelete1[]")(ind), "'", "''")

dEndDate = Replace(Request("mdelete2[]")(ind), "'", "''")
'** Check if specific record exists ****

strSQLExists = "select from buildblanktally where begindate='" & dBeginDate & "' and enddate = '" & dEndDate & "'"

set rsExists = CreateObject("ADODB.Recordset")

rsExists.Open strSQLExists, dbConnection

set rsMeetings = CreateObject("ADODB.Recordset")

set rsTallys = CreateObject("ADODB.Recordset")
if not rsExists.eof then

' if record exists do something

BlankTallyID = rsExists("ID")

If rsExists("SeriesAdded") = 0 then

dim BeginDate, FirstDayNumber, FirstDayName, NumberOfDays,DayCounter, I, X, strSQLWriteTally
BeginDate = rsExists("begindate")

FirstDayNumber = Weekday(rsExists("begindate"))

FirstDayName = WeekDayName(FirstDayNumber, False)

NumberOfDays = DateDiff("d",rsExists("begindate"), rsExists("enddate"))+1

DayCounter= FirstDayNumber
For I = FirstDayNumber to (NumberOfDays+FirstDayNumber)-1

strSQLMeetings ="SELECT
FROM Meetings WHERE (inactive = 0) and (dayid = " & DayCounter & ") ORDER BY timeid"

rsMeetings.Open strSQLMeetings, dbConnection,1,3

NumMeetings = rsMeetings.RecordCount

for MeetingLoop = 1 to NumMeetings

if not rsMeetings.eof then

strSQLWriteTally = "insert into Tally ([Meeting Id],[Meeting Date], [Date Entered],[Admin User Id], RentPaid) Values (" & rsMeetings("ID") & ", '" & BeginDate & "', '" & date & "', '" & session("GroupId") & "', 0)"

rsTallys.Open strSQLWriteTally, dbConnection,1,3

rsMeetings.MoveNext

end if

Next

rsMeetings.Close

DayCounter = DayCounter+1

DayCounter = DayCounter mod 7

If DayCounter = 0 then Daycounter = 7

BeginDate = BeginDate + 1

totalmeetings = totalmeetings + NumMeetings

Next
Response.Write totalmeetings & " tally's were added for the date range selected"

strSQLWriteTally = "update BuildBlankTally set SeriesAdded = 1 where id = " & BlankTallyID

rsTallys.Open strSQLWriteTally, dbConnection,1,3

else

Response.Write "Those Tally's have been previously entered. Please check your dates and try again"

end if
else

' if dont exist do something else

end if
rsExists.Close

set rsTallys = Nothing

set rsMeetings = Nothing

set rsExists = Nothing
BeforeDelete = False

' set BeforeDelete to True if you like to proceed with deleting record

' set it to False in other case
End Function

[/codebox]

Sergey Kornilov admin 9/21/2007

My personal recommendation is to use ASPRunnerPro 5.1 where BeforeDelete event offers you an option to access any field of record to be deleted via dict("FieldName").

This eliminates the need to access form fields directly.