H
Henro
I have a couple of tables that I need to preserve. So I copied those
tables 4 times. At the moment new data is being added I empty table 5,
copy the contents of 4 to 5, I empty 4, copy contents of 3 to 4 and so on.
The code that does that;
<code>
Dim tbl1 As String
Dim tbl2 As String
Dim SelectTblNr As Integer
Dim tblNoNew As Integer
Dim tblNoOld As Integer
Dim DeleteString As String
Dim InsertString As String
tbl1 = "tblKopHenro"
tbl2 = "tblKopAuto"
For tblNoNew = 5 To 2 Step -1
tblNoOld = tblNoNew - 1
DeleteString = "DELETE " & tbl1 & tblNoNew & ".* FROM " & tbl1
& tblNoNew & ";"
InsertString = "INSERT INTO " & tbl1 & tblNoNew & " SELECT " &
tbl1 & tblNoOld & ".* FROM " & tbl1 & tblNoOld & ";"
DoCmd.RunSQL DeleteString
DoCmd.RunSQL InsertString
Next
For tblNoNew = 5 To 2 Step -1
tblNoOld = tblNoNew - 1
DeleteString = "DELETE " & tbl2 & tblNoNew & ".* FROM " & tbl2
& tblNoNew & ";"
InsertString = "INSERT INTO " & tbl2 & tblNoNew & " SELECT " &
tbl2 & tblNoOld & ".* FROM " & tbl2 & tblNoOld & ";"
DoCmd.RunSQL DeleteString
DoCmd.RunSQL InsertString
Next
</code>
But as you see, I have to do that for every table I want to preserve
like that , that will be at last 7 tables, probably more so I need to
use the code from FOR... Next for every tablename, every time changing
the tbl number (tbl1, tbl2, tbl3 and so on).
Now I thought: if I would provide a list of tablenames and use a FOR...
Next to add the numbers to `tbl` I can do with only a few lines of code.
So I made it like this:
<code>
Dim tbl1 As String
Dim tbl2 As String
Dim SelectTblNr As Integer
Dim tblnoNew As Integer
Dim tblNoOld As Integer
Dim DeleteString As String
Dim InsertString As String
Dim Tabelnr As Integer
Dim Tabelnm As string
For Tabelnr = 1 to 2
Tabelnm = "tbl" & Tabelnr
tbl1 = "tblKopHenro"
tbl2 = "tblKopAuto"
For tblnoNew = 5 To 2 Step -1
tblNoOld = tblnoNew - 1
DeleteString = "DELETE " & Tabelnm & tblnoNew & ".* FROM " &
Tabelnm & tblnoNew & ";"
InsertString = "INSERT INTO " & Tabelnm & tblnoNew & " SELECT "
& Tabelnm & tblNoOld & ".* FROM " & Tabelnm & tblNoOld & ";"
DoCmd.RunSQL DeleteString
DoCmd.RunSQL InsertString
Next
Next
</code>
I figured I would use the code above but `Tabelnm` should resolve to
tbl1 which should resolve to "tblKopHenro".
Well, that last thing is not happening! If I halt the code then the
variable 'Tabelnm' is 'tbl1' and not 'tblKopHenro'
How can I do this?
and another thing: I succeeded in finding the tablenames of the tables
to copy using a query:
SELECT Left([Name],Len([Name])-1) AS SelectedTables, MSysObjects.Type
FROM MSysObjects
GROUP BY Left([Name],Len([Name])-1), MSysObjects.Type
HAVING (((Left([Name],Len([Name])-1)) Like "tblkop*") AND
((MSysObjects.Type)=1));
Could that query somehow be used as the source of the list of tables?
(eg tbl1 = [the first out of this query] etc etc)
TIA for thinking!
tables 4 times. At the moment new data is being added I empty table 5,
copy the contents of 4 to 5, I empty 4, copy contents of 3 to 4 and so on.
The code that does that;
<code>
Dim tbl1 As String
Dim tbl2 As String
Dim SelectTblNr As Integer
Dim tblNoNew As Integer
Dim tblNoOld As Integer
Dim DeleteString As String
Dim InsertString As String
tbl1 = "tblKopHenro"
tbl2 = "tblKopAuto"
For tblNoNew = 5 To 2 Step -1
tblNoOld = tblNoNew - 1
DeleteString = "DELETE " & tbl1 & tblNoNew & ".* FROM " & tbl1
& tblNoNew & ";"
InsertString = "INSERT INTO " & tbl1 & tblNoNew & " SELECT " &
tbl1 & tblNoOld & ".* FROM " & tbl1 & tblNoOld & ";"
DoCmd.RunSQL DeleteString
DoCmd.RunSQL InsertString
Next
For tblNoNew = 5 To 2 Step -1
tblNoOld = tblNoNew - 1
DeleteString = "DELETE " & tbl2 & tblNoNew & ".* FROM " & tbl2
& tblNoNew & ";"
InsertString = "INSERT INTO " & tbl2 & tblNoNew & " SELECT " &
tbl2 & tblNoOld & ".* FROM " & tbl2 & tblNoOld & ";"
DoCmd.RunSQL DeleteString
DoCmd.RunSQL InsertString
Next
</code>
But as you see, I have to do that for every table I want to preserve
like that , that will be at last 7 tables, probably more so I need to
use the code from FOR... Next for every tablename, every time changing
the tbl number (tbl1, tbl2, tbl3 and so on).
Now I thought: if I would provide a list of tablenames and use a FOR...
Next to add the numbers to `tbl` I can do with only a few lines of code.
So I made it like this:
<code>
Dim tbl1 As String
Dim tbl2 As String
Dim SelectTblNr As Integer
Dim tblnoNew As Integer
Dim tblNoOld As Integer
Dim DeleteString As String
Dim InsertString As String
Dim Tabelnr As Integer
Dim Tabelnm As string
For Tabelnr = 1 to 2
Tabelnm = "tbl" & Tabelnr
tbl1 = "tblKopHenro"
tbl2 = "tblKopAuto"
For tblnoNew = 5 To 2 Step -1
tblNoOld = tblnoNew - 1
DeleteString = "DELETE " & Tabelnm & tblnoNew & ".* FROM " &
Tabelnm & tblnoNew & ";"
InsertString = "INSERT INTO " & Tabelnm & tblnoNew & " SELECT "
& Tabelnm & tblNoOld & ".* FROM " & Tabelnm & tblNoOld & ";"
DoCmd.RunSQL DeleteString
DoCmd.RunSQL InsertString
Next
Next
</code>
I figured I would use the code above but `Tabelnm` should resolve to
tbl1 which should resolve to "tblKopHenro".
Well, that last thing is not happening! If I halt the code then the
variable 'Tabelnm' is 'tbl1' and not 'tblKopHenro'
How can I do this?
and another thing: I succeeded in finding the tablenames of the tables
to copy using a query:
SELECT Left([Name],Len([Name])-1) AS SelectedTables, MSysObjects.Type
FROM MSysObjects
GROUP BY Left([Name],Len([Name])-1), MSysObjects.Type
HAVING (((Left([Name],Len([Name])-1)) Like "tblkop*") AND
((MSysObjects.Type)=1));
Could that query somehow be used as the source of the list of tables?
(eg tbl1 = [the first out of this query] etc etc)
TIA for thinking!