Bruce said:
Dirk,
This is the code I have so far. So far this works and the array myArr
contains the elements that I pass to the SQL below.
The elements in myArr exist in one of my tables already so I would
like to fill an array with these instead of hard coding, this is
because there are 1,500 records in the table).
Any ideas,
Sub myAppend()
Dim myString As String, myDel As String
Dim myArr
Dim myStartDate As Double
myArr = Array("NAB", "CBA", "ANZ")
myStartDate = startDate(-10)
DoCmd.SetWarnings False
myDel = "DELETE tbl_Volatility.* FROM tbl_Volatility;"
DoCmd.RunSQL myDel
For Each mySecurity In myArr
myString = "INSERT INTO tbl_Volatility ( ASXCode,
Close_H, Close_L, Volatility )" & _
"SELECT myCode(""" & mySecurity & """), Close_H(""" &
mySecurity & """," & myStartDate & "), Close_L(""" & mySecurity &
"""," & myStartDate & "), Volatility(""" & mySecurity & """," &
myStartDate & ");" DoCmd.RunSQL myString
Next mySecurity
DoCmd.SetWarnings True
MsgBox ("Done!")
End Sub
Here's a recordset-based revision, based on the assumption that you have
a table named "tblSecurities" with field "Security" containing your
data:
'----- start of code version 2 -----
Sub myAppend2()
On Error GoTo Err_Handler
Dim myString As String, myDel As String
Dim myArr
Dim myStartDate As Double
Dim rs As DAO.Recordset
myStartDate = startDate(-10)
DoCmd.SetWarnings False
myDel = "DELETE tbl_Volatility.* FROM tbl_Volatility;"
DoCmd.RunSQL myDel
Set rs = CurrentDb.OpenRecordset("tblSecurities")
With rs
Do Until .EOF
myString = _
"INSERT INTO tbl_Volatility (" & _
"ASXCode, Close_H, Close_L, Volatility) " & _
"SELECT myCode(""" & !Security & """), " & _
"Close_H(""" & !Security & """," & myStartDate & _
"), " & _
"Close_L(""" & !Security & """," & myStartDate & _
"), " & _
"Volatility(""" & !Security & """," & myStartDate & _
");"
DoCmd.RunSQL myString
.MoveNext
Loop
.Close
End With
Set rs = Nothing
MsgBox ("Done!")
Exit_Point:
DoCmd.SetWarnings True
Exit Sub
Err_Handler:
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Point
End Sub
'----- end of code version 2 -----
Here's an even simpler, SQL-based version base on the same assumptions.
This ought to work, and be more efficient, provided that myCode(),
Close_H(), Close_L(), and Volatility() are all public functions in a
standard module:
'----- start of code version 3 -----
Sub myAppend3()
On Error GoTo Err_Handler
Dim myString As String, myDel As String
Dim myArr
Dim myStartDate As Double
myStartDate = startDate(-10)
DoCmd.SetWarnings False
myDel = "DELETE tbl_Volatility.* FROM tbl_Volatility;"
DoCmd.RunSQL myDel
myString = _
"INSERT INTO tbl_Volatility " & _
"(ASXCode, Close_H, Close_L, Volatility) " & _
"SELECT " & _
"myCode([Security]), " & _
"Close_H([Security]," & myStartDate & "), " & _
"Close_L([Security]," & myStartDate & "), " & _
"Volatility([Security]," & myStartDate & ") " & _
"FROM tblSecurities;"
DoCmd.RunSQL myString
MsgBox ("Done!")
Exit_Point:
DoCmd.SetWarnings True
Exit Sub
Err_Handler:
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Point
End Sub
'----- end of code version 3 -----
--
Dirk Goldgar, MS Access MVP
www.datagnostics.com
(please reply to the newsgroup)