Afternoon All!
Quick question, but it's got me stumped so I'm hoping there's a Genius out there who can help me out.
What I have is a table "tbl1stAttempt" with my initial data.
I've put this into an array "varArray"
I have an SQL statement
That looks at the table then brings back the corresponding record.
Only problem is that the rest of my code no longer updates the table, it goes straight to the array. What would I do to the code above to enable me to bring back values stored in varArray?
[Weeks Waited] is varArray(0,X)
[Patients Waiting] is varArray(1,X)
[WeeksSinceApr06] is varArray(2,X)
[Spec_ID] is varArray(3,X)
I'll give you a little background - apologies for the length of this - I posted elsewhere but not got a response yet an I'm desperate!
Ok, here we go:
This is for a waiting list simulator over a 52 week period for 19 different surgical specialtys project X amount of weeks into the future. Currently we are trying to project over 12 weeks.
The process is (so far anyway)
I get my list of patients by the weeks they have been waiting, specialty and weeks since april 06 (starts at 52)
I cannot change this data as it comes from other DBs which I'm not allowed to ask about let alone touch.
This goes into tbl1stAttempt (after it has been cleaned a little) in this format:
Weeks Waited Patients Waiting WeeksSinceApr06 Spec_ID
52 11 52 1
51 19 52 1
50 69 52 1
..... .... .... ....
etc etc so we have 1007 rows of data ( 53 week breakdown (0 to 52) by 19 Specialty codes)
I then have another table "tbl_Patients_To_Remove" that has the following fields:
Patients_To_Add
Patients_To_Remove
Specialty_Code
WeeksSinceApril06
The code I have so far looks up the patients to remove from tbl_Patients_To_Remove and removes them from the waiting list, starting with people who have been waiting the longest (week 52). Obviously we cannot have a negative number of people waiting so if the number to remove is greater than the amount of people waiting it will keep going through the list (for that specialty) until it produces a positive number. As we are now simulating a week in the future everything shift up one week (so week waited 51 becomes 52 etc) and week 53 is deleted (after being added to any value in week 52) and the figure in Patients_To_Add is put in week 0.
Wow, that was a bit of a waffle, sorry! Oh dear, here comes another load...
I've got the whole thing working using recordsets and SQL calls to update the table as it loops through the 1007 records another 11 times. The only problem I have is that it takes 15 minutes to update the table as it calls the SQL statement at the end 11077 times! This is why I need to switch it to an array.
I'm nearly there, it's just getting the sql to be able to reference a field in an array that I'm stuck with
I'll post the code I have so far (it's a bit messy as I'm changing thing continuously trying to get it to work)
Brace yourself!
I need to change all the references in tbl1stAttempt (inside the For ... Next loops) to look at varArray instead.
Thanks for looking!!
Quick question, but it's got me stumped so I'm hoping there's a Genius out there who can help me out.
What I have is a table "tbl1stAttempt" with my initial data.
I've put this into an array "varArray"
I have an SQL statement
Code:
sqlNewValue3 = "SELECT [Patients Waiting] " _
& "FROM tbl1stAttempt " _
& "WHERE [Weeks Waited] = " & X - 1 & " AND [WeeksSinceApr06] = " & WeekY - 1 _
& " AND [Spec_ID] = " & SID
That looks at the table then brings back the corresponding record.
Only problem is that the rest of my code no longer updates the table, it goes straight to the array. What would I do to the code above to enable me to bring back values stored in varArray?
[Weeks Waited] is varArray(0,X)
[Patients Waiting] is varArray(1,X)
[WeeksSinceApr06] is varArray(2,X)
[Spec_ID] is varArray(3,X)
I'll give you a little background - apologies for the length of this - I posted elsewhere but not got a response yet an I'm desperate!
Ok, here we go:
This is for a waiting list simulator over a 52 week period for 19 different surgical specialtys project X amount of weeks into the future. Currently we are trying to project over 12 weeks.
The process is (so far anyway)
I get my list of patients by the weeks they have been waiting, specialty and weeks since april 06 (starts at 52)
I cannot change this data as it comes from other DBs which I'm not allowed to ask about let alone touch.
This goes into tbl1stAttempt (after it has been cleaned a little) in this format:
Weeks Waited Patients Waiting WeeksSinceApr06 Spec_ID
52 11 52 1
51 19 52 1
50 69 52 1
..... .... .... ....
etc etc so we have 1007 rows of data ( 53 week breakdown (0 to 52) by 19 Specialty codes)
I then have another table "tbl_Patients_To_Remove" that has the following fields:
Patients_To_Add
Patients_To_Remove
Specialty_Code
WeeksSinceApril06
The code I have so far looks up the patients to remove from tbl_Patients_To_Remove and removes them from the waiting list, starting with people who have been waiting the longest (week 52). Obviously we cannot have a negative number of people waiting so if the number to remove is greater than the amount of people waiting it will keep going through the list (for that specialty) until it produces a positive number. As we are now simulating a week in the future everything shift up one week (so week waited 51 becomes 52 etc) and week 53 is deleted (after being added to any value in week 52) and the figure in Patients_To_Add is put in week 0.
Wow, that was a bit of a waffle, sorry! Oh dear, here comes another load...
I've got the whole thing working using recordsets and SQL calls to update the table as it loops through the 1007 records another 11 times. The only problem I have is that it takes 15 minutes to update the table as it calls the SQL statement at the end 11077 times! This is why I need to switch it to an array.
I'm nearly there, it's just getting the sql to be able to reference a field in an array that I'm stuck with
I'll post the code I have so far (it's a bit messy as I'm changing thing continuously trying to get it to work)
Brace yourself!
Code:
Private Sub Command3_Click()
Dim DB As Database
Dim rst As Recordset
Dim X As Integer
Dim Y As Integer
Dim Z As Integer
Dim SID As Integer
Dim WeekY As Integer
Dim Pats As Integer
Dim NoToRemove As Integer
Dim NoToAdd As Integer
Dim myRemove As Recordset
Dim myAdd As Recordset
Dim myValue As Recordset
Dim myValue2 As Recordset
Dim myValue3 As Recordset
Dim NewValue
Dim NewValue2
Dim NewValue3
Dim MyNewValue
Dim sqlMakeTable As String
Dim sqlRemove As String
Dim sqlAdd As String
Dim sqlNewValue As String
Dim sqlNewValue2 As String
Dim sqlNewValue3 As String
Dim ProgAmount As Integer
Dim RetVal As Variant
Dim varArray()
Dim S As String
Dim intArrayCount As Integer
Dim intArrayCount2 As Integer
Dim intCounter As Long
sqlMakeTable = "SELECT tbl1stAttempt.[Weeks Waited], tbl1stAttempt.[Patients Waiting], tbl1stAttempt.Spec_ID, " _
& "tbl1stAttempt.WeeksSinceApr06, tbl1stAttempt.Spec_Code, tbl1stAttempt.Spec_Desc INTO tblTemp" _
& " FROM tbl1stAttempt, qryMaxWeek " _
& "WHERE (((tbl1stAttempt.[Weeks Waited]) = 0) And ((tbl1stAttempt.Spec_ID) = 3) And ((tbl1stAttempt.WeeksSinceApr06) = 52))" _
& "ORDER BY tbl1stAttempt.[Weeks Waited] DESC;"
DoCmd.SetWarnings False
DoCmd.RunSQL sqlMakeTable
DoCmd.RunSQL ("DELETE * FROM tblTemp")
DoCmd.SetWarnings True
ProgAmount = 1
RetVal = SysCmd(acSysCmdInitMeter, "Simulating Waiting List.....", 11077)
Set DB = CurrentDb
Set rst = DB.OpenRecordset("tbl1stAttempt")
intArrayCount = 0
intCounter = 3
intArrayCount2 = rst.RecordCount
varArray = rst.GetRows()
rst.MoveFirst
X = varArray(0, intArrayCount)
MyNewValue = varArray(1, intArrayCount)
WeekY = varArray(2, intArrayCount)
SID = varArray(3, intArrayCount)
For SID = 1 To 19 Step 1
For WeekY = 53 To 64 Step 1
sqlRemove = "SELECT [Patients_To_Remove] FROM [tbl_Patients_To_Remove] WHERE [WeeksSinceApril06] = " & WeekY & " And [Specialty_Code] = " & SID
Set myRemove = CurrentDb.OpenRecordset(sqlRemove, dbReadOnly)
NoToRemove = myRemove![Patients_To_Remove]
sqlAdd = "SELECT [Patients_To_Add] FROM [tbl_Patients_To_Remove] WHERE [WeeksSinceApril06] = " & WeekY & " And [Specialty_Code] = " & SID
Set myAdd = CurrentDb.OpenRecordset(sqlAdd, dbReadOnly)
NoToAdd = myAdd![Patients_To_Add]
For X = 50 To 0 Step -1
If X = 52 Then
sqlNewValue = "SELECT [Patients Waiting] " _
& "FROM tbl1stAttempt " _
& "WHERE [Weeks Waited] = 52 " _
& "AND [WeeksSinceApr06] = " & WeekY - 1 & " AND [Spec_ID] = " & SID
Set myValue = CurrentDb.OpenRecordset(sqlNewValue, dbOpenDynaset)
sqlNewValue2 = "SELECT [Patients Waiting] " _
& "FROM tbl1stAttempt " _
& "WHERE [Weeks Waited] = 51 " _
& "AND [WeeksSinceApr06] = " & WeekY - 1 & " AND [Spec_ID] = " & SID
Set myValue2 = CurrentDb.OpenRecordset(sqlNewValue2, dbOpenDynaset)
NewValue = myValue![Patients Waiting]
NewValue2 = myValue2![Patients Waiting]
MyNewValue = NewValue + NewValue2
ElseIf X = 0 Then
MyNewValue = NoToAdd
Else:
sqlNewValue3 = "SELECT [Patients Waiting] " _
& "FROM tbl1stAttempt " _
& "WHERE [Weeks Waited] = " & X - 1 & " AND [WeeksSinceApr06] = " & WeekY - 1 _
& " AND [Spec_ID] = " & SID
Set myValue3 = CurrentDb.OpenRecordset(sqlNewValue3, dbOpenDynaset)
MyNewValue = myValue3![Patients Waiting]
End If
If NoToRemove > 0 Then
If NoToRemove > MyNewValue Then
NoToRemove = NoToRemove - MyNewValue
MyNewValue = 0
ElseIf NoToRemove < MyNewValue Then
MyNewValue = MyNewValue - NoToRemove
NoToRemove = 0
End If
End If
DoEvents
varArray(0, intArrayCount) = X
varArray(1, intArrayCount) = MyNewValue
varArray(2, intArrayCount) = WeekY
varArray(3, intArrayCount) = SID
'ReDim Preserve varArray(3, intArrayCount)
'S = "INSERT INTO tbl1stAttempt ([Weeks Waited], [Patients Waiting], WeeksSinceApr06, Spec_ID)" _
' & " VALUES (" & X & ", " & MyNewValue & ", " & WeekY & ", " & SID & ")"
'DoCmd.SetWarnings False
' DoCmd.RunSQL S
'DoCmd.SetWarnings True
intArrayCount = intArrayCount + 1
ReDim Preserve varArray(5, intArrayCount)
RetVal = SysCmd(acSysCmdUpdateMeter, ProgAmount)
ProgAmount = ProgAmount + 1
Next X
DoEvents
Next WeekY
DoEvents
Next SID
DoEvents
RetVal = SysCmd(acSysCmdRemoveMeter)
'DoCmd.SetWarnings False
' DoCmd.OpenQuery "qryOutPut"
'DoCmd.SetWarnings True
MsgBox ("Simulation Complete...!")
DoCmd.Close acForm, "frmMain"
Set rst = Nothing
End Sub
I need to change all the references in tbl1stAttempt (inside the For ... Next loops) to look at varArray instead.
Thanks for looking!!