Writing a routine to create sequential records - sql

I would like to write a routine which will allow me to take dated events (records) in a table which span accross a set time frame and in the cases where no event took place for a specific day, an event will be created duplicating the most recent prior record where an event DID take place.
For example: If on September 4 Field 1 = X, Field 2 = Y and Field 3 = Z and then nothing took place until September 8 where Field 1 = Y, Field 2 = Z and Field 3 = X, the routine would create records in the table to account for the 3 days where nothing took place and ultimately return a table looking like:
Sept 4: X - Y - Z
Sept 5: X - Y - Z
Sept 6: X - Y - Z
Sept 7: X - Y - Z
Sept 8: Y - Z - X
Unfortunately, my level of programming knowledge although good, does not allow me to logically conclude a solution in this case. My gut feeling tells me that a loop could be the correct solution here but I still an not sure exactly how. I just need a bit of guidance to get me started.

Here you go.
Sub FillBlanks()
Dim rsEvents As Recordset
Dim EventDate As Date
Dim Fld1 As String
Dim Fld2 As String
Dim Fld3 As String
Dim SQL As String
Set rsEvents = CurrentDb.OpenRecordset("SELECT * FROM tblevents ORDER BY EventDate")
'Save the current date & info
EventDate = rsEvents("EventDate")
Fld1 = rsEvents("Field1")
Fld2 = rsEvents("Field2")
Fld3 = rsEvents("Field3")
rsEvents.MoveNext
On Error Resume Next
Do
' Loop through each blank date
Do While EventDate < rsEvents("EventDate") - 1 'for all dates up to, but not including the next date
EventDate = EventDate + 1 'advance date by 1 day
rsEvents.AddNew
rsEvents("EventDate") = EventDate
rsEvents("Field1") = Fld1
rsEvents("Field2") = Fld2
rsEvents("Field3") = Fld3
rsEvents.Update
Loop
' get new current date & info
EventDate = rsEvents("EventDate")
Fld1 = rsEvents("Field1")
Fld2 = rsEvents("Field2")
Fld3 = rsEvents("Field3")
rsEvents.MoveNext
' new records are placed on the end of the recordset,
' so if we hit on older date, we know it's a recent insert and quit
Loop Until rsEvents.EOF Or EventDate > rsEvents("EventDate")
End Sub

With no details about your specifics (table schema, available language options etc), iI guess that you just need the algorithm to pick up. So here's a quick algorithm with no safeguards.
properdata = "select * from data where eventHasTakenPlace=true";
wrongdata = "select * from data where eventHasTakenPlace=false";
for each wrongRecord in wrongdata {
exampleRecord = select a.value1, a.value2,...,a.date from properdata as a
inner join
(select id,max(date)
from properdata
group by id
having date<wrongRecord.date
) as b
on a.id=b.id
minDate = exampleRecord.date;
maxDate = wrongRecord.date -1day; --use proper date difference function as per your language of choice.
for i=minDate to maxDate step 1day{
dynamicsql="INSERT INTO TABLE X(Value1,Value2....,date) VALUES (exampleRecord.Value1, exampleRecord.Value2,...i);
exec dynamicsql;
}
}

Private Sub Command109_Click()
On Error GoTo errhandler
Dim rsEvents As Recordset
Dim EventDate As Date
Dim ProjID As String
Dim Fld1 As String
Dim Fld2 As String
Dim Fld3 As String
Dim Fld4 As String
Dim Fld5 As String
Dim Fld6 As String
Dim Fld7 As String
Dim Fld8 As String
Dim Fld9 As String
Dim Fld10 As String
Dim Fld11 As String
Dim Fld12 As String
Dim Fld13 As String
Dim Fld14 As String
Dim Fld15 As String
Dim Fld16 As String
Dim Fld17 As String
Dim Fld18 As String
Dim Fld19 As String
Dim Fld20 As String
Dim Fld21 As String
Dim st_sql As String
Dim Sql As String
Me.Refresh
Set rsEvents = CurrentDb.OpenRecordset("SELECT * FROM tblProjectMasterListHistory02 ORDER BY LastUpdateDate")
'Save the current date and info
EventDate = rsEvents("LastUpdateDate")
ProjID = rsEvents("ID Project")
Fld1 = rsEvents("OverallPrincipleStatus1")
Fld2 = rsEvents("OverallPrincipleStatus2")
Fld3 = rsEvents("OverallObjectiveStatus")
Fld4 = rsEvents("OverallObjectiveStatus2")
Fld5 = rsEvents("OverallDependencyStatus1")
Fld6 = rsEvents("OverallDependencyStatus2")
Fld7 = rsEvents("OverallAssumptionsStatus1")
Fld8 = rsEvents("OverallAssumptionsStatus2")
Fld9 = rsEvents("OverallConstraintsStatus1")
Fld10 = rsEvents("OverallConstraintsStatus2")
Fld11 = rsEvents("ObjectivesScope")
Fld12 = rsEvents("ObjectivesResources")
Fld13 = rsEvents("ObjectivesProjectPlan")
Fld14 = rsEvents("ObjectivesEffort")
Fld15 = rsEvents("ObjectivesBenefits")
Fld16 = rsEvents("ObjectivesResourceMobilisation")
Fld17 = rsEvents("ObjectivesMetrics")
Fld18 = rsEvents("OverallRiskStatus1")
Fld19 = rsEvents("OverallRiskStatus2")
Fld20 = rsEvents("GovernanceStatus1")
Fld21 = rsEvents("GovernanceStatus2")
rsEvents.MoveNext
Do
' Loop through each blank date
Do While EventDate < rsEvents("LastUpdateDate") - 1 'for all dates up to, but not including the next date
EventDate = EventDate + 1 'advance date by 1 day
rsEvents.AddNew
rsEvents("LastUpdateDate") = EventDate
rsEvents("ID Project") = ProjID
rsEvents("OverallPrincipleStatus1") = Fld1
rsEvents("OverallPrincipleStatus2") = Fld2
rsEvents("OverallObjectiveStatus") = Fld3
rsEvents("OverallObjectiveStatus2") = Fld4
rsEvents("OverallDependencyStatus1") = Fld5
rsEvents("OverallDependencyStatus2") = Fld6
rsEvents("OverallAssumptionsStatus1") = Fld7
rsEvents("OverallAssumptionsStatus2") = Fld8
rsEvents("OverallConstraintsStatus1") = Fld9
rsEvents("OverallConstraintsStatus2") = Fld10
rsEvents("ObjectivesScope") = Fld11
rsEvents("ObjectivesResources") = Fld12
rsEvents("ObjectivesProjectPlan") = Fld13
rsEvents("ObjectivesEffort") = Fld14
rsEvents("ObjectivesBenefits") = Fld15
rsEvents("ObjectivesResourceMobilisation") = Fld16
rsEvents("ObjectivesMetrics") = Fld17
rsEvents("OverallRiskStatus1") = Fld18
rsEvents("OverallRiskStatus2") = Fld19
rsEvents("GovernanceStatus1") = Fld20
rsEvents("GovernanceStatus2") = Fld21
rsEvents.Update
Loop
' get new current date and info
EventDate = rsEvents("LastUpdateDate")
ProjID = rsEvents("ID Project")
Fld1 = rsEvents("OverallPrincipleStatus1")
Fld2 = rsEvents("OverallPrincipleStatus2")
Fld3 = rsEvents("OverallObjectiveStatus")
Fld4 = rsEvents("OverallObjectiveStatus2")
Fld5 = rsEvents("OverallDependencyStatus1")
Fld6 = rsEvents("OverallDependencyStatus2")
Fld7 = rsEvents("OverallAssumptionsStatus1")
Fld8 = rsEvents("OverallAssumptionsStatus2")
Fld9 = rsEvents("OverallConstraintsStatus1")
Fld10 = rsEvents("OverallConstraintsStatus2")
Fld11 = rsEvents("ObjectivesScope")
Fld12 = rsEvents("ObjectivesResources")
Fld13 = rsEvents("ObjectivesProjectPlan")
Fld14 = rsEvents("ObjectivesEffort")
Fld15 = rsEvents("ObjectivesBenefits")
Fld16 = rsEvents("ObjectivesResourceMobilisation")
Fld17 = rsEvents("ObjectivesMetrics")
Fld18 = rsEvents("OverallRiskStatus1")
Fld19 = rsEvents("OverallRiskStatus2")
Fld20 = rsEvents("GovernanceStatus1")
Fld21 = rsEvents("GovernanceStatus2")
rsEvents.MoveNext
'new records are placed on the end of the recordset
'so if we hit an older date, we know it's a recent insert and quit
Loop Until rsEvents.EOF Or EventDate > rsEvents("LastUpdateDate")
errhandler:
End Sub

Related

How to check if an item in a table is between two times M:N

I have a many to many relationship between two entities. Computer and Booking, with the link entity being BookingToComputer which contains bookingID and computerID. Im trying to check that the computer is available for booking before adding it to the tables.
So I've selected the date, time, and hours of use (3 attributes in the booking table) of existing Booking records. I've created two time variable, start time (totalTime) and the end time of a booking (endtime). I've tried to put a loop to where each item in my list would go through all the selected rows from a datagridview to check if they match but I don't think I got it quite right and at this point im super confused.
Dim Part1 As String = txtTime.Text + ":00:00"
Dim Part2 As String = "00:" + txtTime1.Text + ":00"
Dim Part3 As String = "00:00:" + txtTime2.Text
Dim Time1 As TimeSpan = TimeSpan.Parse(Part1)
Dim Time2 As TimeSpan = TimeSpan.Parse(Part2)
Dim Time3 As TimeSpan = TimeSpan.Parse(Part3)
Dim TotalTime As TimeSpan = Time1 + Time2 + Time3
SQL.AddParam("#Time", txtTime.Text & ":" & txtTime1.Text & ":" & txtTime2.Text)
SQL.AddParam("#Date", txtDate.Text)
SQL.ExecQuery("SELECT bc.Computer_ID, hours_of_use, time_of_use FROM BookingToComputer AS bc, Computer AS c, Booking AS b
WHERE bc.Booking_ID = b.Booking_ID AND c.Computer_ID = bc.Computer_ID AND b.date_of_use = #Date AND b.time_of_use <= #Time;")
DataGridView2.DataSource = SQL.DBDT
If DataGridView2.Rows.Count() > 0 Then
'cell1 contains the hours used and time contains the time'
Dim TimeInt As Integer = DataGridView2.Rows(0).Cells(1).Value
Dim TimeInt2 As TimeSpan = DataGridView2.Rows(0).Cells(2).Value
Dim Addit As String = TimeInt.ToString + ":00:00"
Dim interv As TimeSpan = TimeSpan.Parse(Addit)
Dim endtime As TimeSpan = TimeInt2 + interv
Dim rowpass As Integer = 0
For Each x In listpick
Dim CompID As Integer = DataGridView2.Rows(rowpass).Cells(0).Value
If x = CompID Then
If endtime > TotalTime Then
MsgBox(x & " is already reserved.")
Else
MsgBox("diff time, same comp")
End If
Else
MsgBox("Good time - diff comp")
End If
rowpass = rowpass + 1
Next
End If

VB.NET - Given a date, how can I get the date of last four fridays?

Given today's date want to get the date of the each Friday for the last four weeks.
Here is an easy LINQ approach:
Dim today = Date.Today
Dim lastFridays = From d In Enumerable.Range(0, Int32.MaxValue)
Let dt = today.AddDays(-d)
Where dt.DayOfWeek = DayOfWeek.Friday
Select dt
Dim lastFourFridays As Date() = lastFridays.Take(4).ToArray()
Since it's not the most efficient approach, here is a query that is still readable and maintainable but only searches the first friday and then takes only every 7th day:
Dim lastFriday = lastFridays.First() ' reuse of above query '
Dim fridays = From d In Enumerable.Range(0, Int32.MaxValue)
Let dt = lastFriday.AddDays(-d * 7)
Select dt
Dim lastFourFridays As Date() = fridays.Take(4).ToArray()
You may consume this one, which returns a list of such dates and excludes the one if the specifiedDate date is Friday:
Public Shared Function GetLastFourFridays(specifiedDate As DateTime) As List(Of DateTime)
Dim dtm As New List(Of DateTime)()
Dim dt As DateTime = specifiedDate
For i As Integer = 0 To 6
dt = dt.AddDays(-1)
If dt.DayOfWeek = DayOfWeek.Friday Then
dtm.Add(dt)
Exit For
End If
Next
dtm.Add(dt.AddDays(-7))
dtm.Add(dt.AddDays(-14))
dtm.Add(dt.AddDays(-21))
Return dtm
End Function
and the way you use it is:
Dim dtm As List(Of DateTime) = GetLastFourFridays(DateTime.Now)
For Each d As var In dtm
Console.WriteLine(String.Format("Date: {0}, Day: {1}", d.ToString(), [Enum].Parse(GetType(DayOfWeek), d.DayOfWeek.ToString())))
Next
Here is my way:
Function Last4Friday(ByVal StartDate As Date) As array
Dim L4F()
Dim mDate as date = StartDate
For value As Integer = 1 To 7
mDate = mDate.AddDays(-1)
If mDate.DayOfWeek = DayOfWeek.Friday Then
L4F = {mDate, mDate.AddDays(-7), mDate.AddDays(-14), mDate.AddDays(-21)}
exit for
End If
Next
Return L4F
End Function
Edit: If you need to check the inserted date and you want it returned in the array you may simply use:
Dim mDate as date = StartDate.AddDays(1)
instead of
Dim mDate as date = StartDate
Try this. It doesn't use a loop to find the starting Friday.
Dim someDate As DateTime = DateTime.Now
If someDate.DayOfWeek <> DayOfWeek.Friday Then
'do the math to get a Friday
someDate = someDate.AddDays(DayOfWeek.Friday - someDate.AddDays(1).DayOfWeek - 6)
End If
Dim last4Fridays As New List(Of DateTime) From {someDate, someDate.AddDays(-7), someDate.AddDays(-14), someDate.AddDays(-21)}
All of the other suggestions have used a loop to find the starting Friday. If this code is used infrequently then how the starting Friday is determined might not matter.
edit: as function
Function FindLastFourFridays(someDate As DateTime) As List(Of DateTime)
'Find first Friday to include
If someDate.DayOfWeek <> DayOfWeek.Friday Then
someDate = someDate.AddDays(DayOfWeek.Friday - someDate.AddDays(1).DayOfWeek - 6)
' uncomment these two lines if you do not want initial someDate.DayOfWeek = DayOfWeek.Friday to be included
'Else
' someDate = someDate.AddDays(-7)
End If
'build the return (four fridays)
Dim last4Fridays As New List(Of DateTime) From {someDate, someDate.AddDays(-7), someDate.AddDays(-14), someDate.AddDays(-21)}
Return last4Fridays
End Function
This function does not need to be passed a date it picks up today's date and gets the last four Friday's from today. It can be changed around to get any day of the week.
Dim todaysDate As Date = Date.Today
Dim oldDay As Integer
Dim thisWeek As Date
Dim firstWeek As Date
Dim secondWeek As Date
Dim thirdWeek As Date
Dim fourthWeek As Date
'finds the Friday of the end of the current week No mattter what day you are working
Dim daycount As Integer
'use this to check specific dates "Dim datetime As New DateTime(2015, 4, 13)"
oldDay = Weekday(todaysDate)
thisWeek = todaysDate
If oldDay < 6 Then
daycount = 6 - oldDay
thisWeek = thisWeek.AddDays(+daycount)
ElseIf oldDay > 6 Then
daycount = oldDay - 6
thisWeek = thisWeek.AddDays(-daycount)
End If
Dim currentDate As Date = Now
Do While Not currentDate.DayOfWeek = DayOfWeek.Friday
currentDate = currentDate.AddDays(-1)
Loop
fourthWeek = currentDate.AddDays(-21)
thirdWeek = currentDate.AddDays(-14)
secondWeek = currentDate.AddDays(-7)
firstWeek = currentDate

Make field value static in access vba

Hello I am trying to add various values based on Select Case to the value of field. The problem I face is that each time when I get different Case in select statement, the value of the field rather changing adds the value on top.
Private Sub ProductID_AfterUpdate()
Dim qflPrice As Variant
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim sqlQry As String
Dim instID As Integer
instID = Me.Form!ProductID.Value
sqlQry = "SELECT Products.Price FROM Products WHERE Products.ProductID = " & instID & ""
Set db = CurrentDb
Set rs = db.OpenRecordset(sqlQry)
Me.flPrice.Value = rs!Price
End Sub
Private Sub ExtrasID_Change()
Dim extrID As Integer
Dim addNum As Integer
Static floorPrice As Integer
Static sumPrice As Integer
extrID = Me.ExtrasID.Value
floorPrice = Me.flPrice.Value
Select Case extrID
Case Is = 1
addNum = 5
sumPrice = floorPrice + addNum
Case Is = 2
addNum = 10
sumPrice = floorPrice + addNum
Case Is = 3
addNum = 15
sumPrice = floorPrice + addNum
End Select
Me.flPrice.Value = sumPrice
End Sub
Me.flPrice.Value = Me.flPrice.Value + qflPrice is the culprit
Replace by: Me.flPrice.Value = qflPrice
By the way, you should clean up your code a bit
Private Sub ExtrasID_Change()
Dim extrID As Integer
Static qflPrice As Integer
Static numPrice As Integer
extrID = Me.ExtrasID.Value
numPrice = Me.flPrice.Value
Select Case extrID
Case Is = 1
qflPrice = 5
Case Is = 2
qflPrice = 10
Case Is = 3
qflPrice = 15
End Select
Msgbox qflPrice
Me.flPrice.Value = qflPrice
End Sub
(you could even just do Me.flPrice.Value = Me.ExtrasID.Value * 5 and not use select statement in your case, but I'm guessing this is just an example)
I Fixed the issue myself by declaring the global variable.
Option Compare Database
Public rsqryPrice As Integer
Option Explicit
Private Sub ProductID_AfterUpdate()
Dim qflPrice As Variant
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim sqlQry As String
Dim instID As Integer
instID = Me.Form!ProductID.Value
sqlQry = "SELECT Products.Price FROM Products WHERE Products.ProductID = " & instID & ""
Set db = CurrentDb
Set rs = db.OpenRecordset(sqlQry)
Me.flPrice.Value = rs!Price
rsqryPrice = rs!Price
End Sub
Private Sub ExtrasID_Change()
Dim extrID As Integer
Static floorPrice As Integer
Static sumPrice As Integer
Static numPrice As Integer
extrID = Me.ExtrasID.Value
floorPrice = Me.flPrice.Value
numPrice = rsqryPrice
Select Case extrID
Case Is = 1
Me.flPrice.Value = numPrice + 5
Case Is = 2
Me.flPrice.Value = numPrice + 10
Case Is = 3
Me.flPrice.Value = numPrice + 15
End Select
End Sub

Run function for multiple data sets and output results to different cells

I have been trying forever to try and figure this out. I have a set of data in a certain sheet in my Excel file. I have written code so that it outputs some of that information to another sheet. I don't know how to get the function to loop through all the different data sets and output them into the "Output" sheet in my excel file on different rows.
This is what I have so far. Can someone please help?
How do I get the function to run through about 6 data sets that include 5 cells in the column until there are 2 blank cells?
How do I output those different results to another sheet? I already have them outputting the first data set and it works fine. I just need to know how to do the other ones.
Thank you!
Sub EstBatch()
'variables
Dim N As String
Dim D As Date
Dim P As Integer
Dim H As Single
Dim NS As Integer
Dim NL As Integer
Dim BP As Currency
Dim OH As Single
Dim OC As Currency
Dim TP As Currency
Dim PPBR As Currency
Dim EHP As Single
Dim batches As Range
'inputs
N = Sheets("Batch Input").Range("A1").Value
D = Sheets("Batch Input").Range("B1").Value
P = Sheets("Batch Input").Range("A2").Value
H = Sheets("Batch Input").Range("A3").Value
PPBR = Sheets("User Form").Range("C22").Value
EHP = Sheets("User Form").Range("C23").Value
Range("A1").Select
'Processes
BP = P * PPBR
OH = H - 5
If P > 120 Or P < 20 Then
MsgBox ("Cannot Accommodate Group")
ElseIf P >= 20 And P <= 25 Then
NS = 1
NL = 0
ElseIf P >= 26 And P <= 50 Then
NS = 2
NL = 0
ElseIf P >= 51 And P <= 60 Then
NS = 0
NL = 1
ElseIf P >= 61 And P <= 85 Then
NS = 1
NL = 1
ElseIf P >= 86 And P <= 120 Then
NS = 0
NL = 2
End If
If OH > 4 Then
OH = 4
OC = BP * OH * EHP
ElseIf 0 < OH <= 4 Then
OC = BP * OH * EHP
ElseIf OH <= 0 Then
OC = 0
End If
TP = BP + OC
'outputs
Sheets("Batch Output").Range("A2").Value = N
Sheets("Batch Output").Range("B2").Value = D
Sheets("Batch Output").Range("C2").Value = P
Sheets("Batch Output").Range("D2").Value = H
Sheets("Batch Output").Range("E2").Value = PPBR
Sheets("Batch Output").Range("F2").Value = EHP
Sheets("Batch Output").Range("G2").Value = NS
Sheets("Batch Output").Range("H2").Value = NL
Sheets("Batch Output").Range("I2").Value = BP
Sheets("Batch Output").Range("J2").Value = OH
Sheets("Batch Output").Range("K2").Value = OC
Sheets("Batch Output").Range("L2").Value = TP
End Sub
Welcome to StackOverflow. Great first question.
I think what you're reaching for is how to use loops in solving a problem like this.
One easy way to do loops is with a counter, as in the examples I've given below. If appropriate, you can also use a range of cells to loop through data, as described in this answer: https://stackoverflow.com/a/19394207/2665195.
Starting with your second question: if you want a separate sheet for each output you can use Sheets.Add and paste into that new sheet. To do this you will want to use a variable naming convention like Sheets("Batch Output" & X).Range. In this way you can Dim X as Integer and loop through the process incrementing the X integer with each loop. Here's some sample code you can adapt for your purpose:
Sub ExampleAddSheets()
Dim intX As Integer
intX = 1
Dim wsBatchOutput As Worksheet
For intX = 1 To 6
Set wsBatchOutput = Worksheets.Add 'adds a worksheet and tags it to a variable
wsBatchOutput.Name = "BatchOutput" & intX 'names the worksheet
wsBatchOutput.Range("A1").Value = "Data here. Example " & intX
Next intX
Set wsBatchOutput = Nothing
End Sub
I don't know what your data source looks like, but hopefully it is set up in a way that you can turn the inputs aquisition into a loop. For example, if the data came into the system in rows (which your example does not seem to do) you could just increment the row number, something like this:
Sub ExampleSetInputs()
'variables
Dim N As String
Dim D As Date
Dim P As Integer
Dim H As Single
Dim PPBR As Currency
Dim EHP As Single
Dim intRow As Integer
intRow = 2
'inputs
For intRow = 2 To 7
N = Sheets("Batch Input").Range("A" & intRow).Value
D = Sheets("Batch Input").Range("B" & intRow).Value
P = Sheets("Batch Input").Range("C" & intRow).Value
H = Sheets("Batch Input").Range("D" & intRow).Value
Next intRow
End Sub
I hope this helps with your challenge.

Inserting to table in VB.NET, LINQ to SQL

This is the beginnings of a timetabling algorithm. The problem is with inserting a member into a group, but I have included the whole subroutine here for context. The table "membergroup" has 2 headings, MemberID and GroupID. No error code is thrown, but the table does not receive the new record.
I have gone through it line by line, and the values for iMember_Choice.MemberID and groupID_to_insert are correct.
Dim numberofrooms As Byte = rm.Count
Dim possiblerooms(numberofrooms + 1), possibleroomcounter As Int32
For TimeTableNumber = 1 To Val(NumberOfTimetablesToCreate.Text)
Dim memchoic = (dc.ExecuteQuery(Of memberChoice)("SELECT * FROM MemberChoices ORDER BY NEWID()")).ToList 'Orders list randomly
'sort into array for each rank, so highest ranks are allocated first
Dim Member_choices_Table_ordered_by_rank = From q In memchoic Order By q.Rank
For Each Member_Choice In Member_choices_Table_ordered_by_rank
ProgressBar.Value = ProgressBar.Value + 1
Dim iMember_Choice As memberChoice = Member_Choice
'Dim memactpossibleinstance(maxmemchoicernk, n) As memactvpossibleinstance
For Each room In rm
Dim rmid As Int32 = room.RoomID ' finds rooms activities can be in
If Not (From rom In roomact Where rom.ActivityID = iMember_Choice.ActivityID And rom.RoomID = rmid).FirstOrDefault Is Nothing Then 'finds suitable rooms
possiblerooms(possibleroomcounter) = rmid
possibleroomcounter = possibleroomcounter + 1
End If
Next
'find possible times
Dim roomid_to_insert, current_maximum_rank As Integer
Dim period_to_insert As String = "MonAM"
Dim staffact_that_can_do_this_activity = _
(From q In staffact Where q.ActivityID = iMember_Choice.ActivityID)
For roomcount = 0 To possibleroomcounter - 1 'for each room in roomcount, find rank for room
Dim rmid As Int32 = possiblerooms(roomcount)
For Each time In periods
Dim itime As String = time.Period
Dim Rank As Int16 = member_Error_check_period_count(iMember_Choice.MemberID, itime)
If Not (From rav In rmav Where rav.RoomID = rmid And rav.Period = itime) Is Nothing Then 'room is free
Rank = Rank + 12
Else ' room has an activity
Dim GroupID_now = (From q In instnce Where q.Period = itime And q.RoomID = rmid Select q.GroupID).SingleOrDefault
If GroupID_now <> 0 Then
Dim groupActID_now = (From q In grp Where q.GroupID = GroupID_now Select q.ActivityID).SingleOrDefault
'Dim activity_now = (From q In actv Where q.ActivityID = groupActID_now).SingleOrDefault
If groupActID_now = iMember_Choice.ActivityID Then 'Good, this activity is already on in this room
Rank = Rank + 50
If (From q In memgrp Where q.GroupID = GroupID_now).Count > 4 Then
Rank = Rank - 50
End If
End If
End If
End If
If Rank > current_maximum_rank Then
current_maximum_rank = Rank
roomid_to_insert = rmid
period_to_insert = itime
End If
Rank = 0
Next
possibleroomcounter = 0
Next 'for each room possible
Dim groupID_to_insert As Integer
If (From q In instnce Where q.Period = period_to_insert And q.RoomID = roomid_to_insert).FirstOrDefault Is Nothing Then
groupID_to_insert = insert_ins_group(period_to_insert, roomid_to_insert, iMember_Choice.ActivityID)
Else
groupID_to_insert = (From q In instnce Where q.Period = period_to_insert And q.RoomID = roomid_to_insert Select q.GroupID).SingleOrDefault
End If
'PROBLEM PROBPBLY HERE/////////////////////////////////////
memgrp.InsertOnSubmit(New membergroup With {.MemberID = iMember_Choice.MemberID, .GroupID = groupID_to_insert}) 'PROBLEM PROBABLY HERE
dc.SubmitChanges()
current_maximum_rank = 0
Next 'for each memberchoice
dc.SubmitChanges()
Next 'timetabl no
dc.SubmitChanges()
memgrp is initiated as
Dim memgrp As Table(Of membergroup) = dc.GetTable(Of membergroup)()