F
FGM
Windows 2000 Access/Office 2002
Below is my code. The end of the code saves the excel file to the pathfile
name. Sometimes it works and sometimes it pops up with the dialoge box and
wants to save it as book1. Why?
I just want it to save the file/path that I am sending with the call to this
routine.
Any help would be appreciated.... Thanks.
Sub sCopyRSToNamedRange(pathfile)
'Copy records to a named range
'on an existing worksheet on a
'workbook
'
Dim strSQL As String
Dim strSQL2 As String
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim strPathFile As String
Dim strFilename As String
Const conRANGE = "A4"
strPathFile = pathfile
strPathFile = pathfile
Set db = CurrentDb
strSQL2 = "SELECT * FROM tblFilenames Where Selection = True"
Set rs2 = db.OpenRecordset(strSQL2)
Set objXL = New Excel.Application
With objXL
.Visible = True
Set objWkb = .Workbooks.Add
End With
rs2.MoveLast
rs2.MoveFirst
Do Until rs2.EOF
strFilename = rs2.Fields("FileName").Value
strSQL = "SELECT * FROM [LN-Levels] Where FileName = '" & strFilename & "'"
Set rs = db.OpenRecordset(strSQL)
On Error Resume Next
Set objSht = objWkb.Worksheets.Add
objSht.Name = "LN-" & rs.Fields("FileName").Value
objSht.Activate
'========Header
objSht.Range("A2").Value = "FileName"
objSht.Range("B2").Value = "REC"
objSht.Range("C2").Value = "DATE"
objSht.Range("D2").Value = "Time"
objSht.Range("E2").Value = "LN_Level"
objSht.Range("F2").Value = "12.5Hz"
objSht.Range("G2").Value = "16Hz"
objSht.Range("H2").Value = "20Hz"
objSht.Range("I2").Value = "25Hz"
objSht.Range("J2").Value = "31.5Hz"
objSht.Range("K2").Value = "40Hz"
objSht.Range("L2").Value = "50Hz"
objSht.Range("M2").Value = "63Hz"
objSht.Range("N2").Value = "80Hz"
objSht.Range("O2").Value = "100Hz"
objSht.Range("P2").Value = "125Hz"
objSht.Range("Q2").Value = "160Hz"
objSht.Range("R2").Value = "200Hz"
objSht.Range("S2").Value = "250Hz"
objSht.Range("T2").Value = "315Hz"
objSht.Range("U2").Value = "400Hz"
objSht.Range("V2").Value = "500Hz"
objSht.Range("W2").Value = "630Hz"
objSht.Range("X2").Value = "800Hz"
objSht.Range("Y2").Value = "1KHz"
objSht.Range("Z2").Value = "1.25KHz"
objSht.Range("AA2").Value = "16KHz"
objSht.Range("AB2").Value = "2KHz"
objSht.Range("AC2").Value = "2.5KHz"
objSht.Range("AD2").Value = "3.15KHz"
objSht.Range("AE2").Value = "4KHz"
objSht.Range("AF2").Value = "5KHz"
objSht.Range("AG2").Value = "6.3KHz"
objSht.Range("AH2").Value = "8KHz"
objSht.Range("AI2").Value = "10KHz"
objSht.Range("AJ2").Value = "12.5KHz"
objSht.Range("AK2").Value = "16KHz"
objSht.Range("AL2").Value = "20KHz"
With xlRange("A2:AL2")
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
'++++++++++ END OF HEADER
objSht.Range(conRANGE).CopyFromRecordset rs
rs2.MoveNext
Loop
' objWkb(strPathFile).Save
objWkb.SaveAs Filename:=strPathFile, FileFormat:=xlNormal, _
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False
objWkb.Close
objXL.Quit
Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing
Set rs = Nothing
Set rs2 = Nothing
Set db = Nothing
End Sub
Below is my code. The end of the code saves the excel file to the pathfile
name. Sometimes it works and sometimes it pops up with the dialoge box and
wants to save it as book1. Why?
I just want it to save the file/path that I am sending with the call to this
routine.
Any help would be appreciated.... Thanks.
Sub sCopyRSToNamedRange(pathfile)
'Copy records to a named range
'on an existing worksheet on a
'workbook
'
Dim strSQL As String
Dim strSQL2 As String
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim strPathFile As String
Dim strFilename As String
Const conRANGE = "A4"
strPathFile = pathfile
strPathFile = pathfile
Set db = CurrentDb
strSQL2 = "SELECT * FROM tblFilenames Where Selection = True"
Set rs2 = db.OpenRecordset(strSQL2)
Set objXL = New Excel.Application
With objXL
.Visible = True
Set objWkb = .Workbooks.Add
End With
rs2.MoveLast
rs2.MoveFirst
Do Until rs2.EOF
strFilename = rs2.Fields("FileName").Value
strSQL = "SELECT * FROM [LN-Levels] Where FileName = '" & strFilename & "'"
Set rs = db.OpenRecordset(strSQL)
On Error Resume Next
Set objSht = objWkb.Worksheets.Add
objSht.Name = "LN-" & rs.Fields("FileName").Value
objSht.Activate
'========Header
objSht.Range("A2").Value = "FileName"
objSht.Range("B2").Value = "REC"
objSht.Range("C2").Value = "DATE"
objSht.Range("D2").Value = "Time"
objSht.Range("E2").Value = "LN_Level"
objSht.Range("F2").Value = "12.5Hz"
objSht.Range("G2").Value = "16Hz"
objSht.Range("H2").Value = "20Hz"
objSht.Range("I2").Value = "25Hz"
objSht.Range("J2").Value = "31.5Hz"
objSht.Range("K2").Value = "40Hz"
objSht.Range("L2").Value = "50Hz"
objSht.Range("M2").Value = "63Hz"
objSht.Range("N2").Value = "80Hz"
objSht.Range("O2").Value = "100Hz"
objSht.Range("P2").Value = "125Hz"
objSht.Range("Q2").Value = "160Hz"
objSht.Range("R2").Value = "200Hz"
objSht.Range("S2").Value = "250Hz"
objSht.Range("T2").Value = "315Hz"
objSht.Range("U2").Value = "400Hz"
objSht.Range("V2").Value = "500Hz"
objSht.Range("W2").Value = "630Hz"
objSht.Range("X2").Value = "800Hz"
objSht.Range("Y2").Value = "1KHz"
objSht.Range("Z2").Value = "1.25KHz"
objSht.Range("AA2").Value = "16KHz"
objSht.Range("AB2").Value = "2KHz"
objSht.Range("AC2").Value = "2.5KHz"
objSht.Range("AD2").Value = "3.15KHz"
objSht.Range("AE2").Value = "4KHz"
objSht.Range("AF2").Value = "5KHz"
objSht.Range("AG2").Value = "6.3KHz"
objSht.Range("AH2").Value = "8KHz"
objSht.Range("AI2").Value = "10KHz"
objSht.Range("AJ2").Value = "12.5KHz"
objSht.Range("AK2").Value = "16KHz"
objSht.Range("AL2").Value = "20KHz"
With xlRange("A2:AL2")
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
'++++++++++ END OF HEADER
objSht.Range(conRANGE).CopyFromRecordset rs
rs2.MoveNext
Loop
' objWkb(strPathFile).Save
objWkb.SaveAs Filename:=strPathFile, FileFormat:=xlNormal, _
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False
objWkb.Close
objXL.Quit
Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing
Set rs = Nothing
Set rs2 = Nothing
Set db = Nothing
End Sub