I want the user to be able to select the file to update on there shared drive by selecting a value from listbox 1 on my form.
Currently, my code always updates: Const conWKB_NAME = "c:\temp\book1.xls".
How do I set Const conWKB_NAME = listbox1 value on form1?
Code:
Sub sCopyRSToNamedRange()
'Copy records to a named range
'on an existing worksheet on a
'workbook
'
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim db As Database
Dim rs As Recordset
Const conMAX_ROWS = 20000
Const conSHT_NAME = "Sheet1"
Const conWKB_NAME = "c:\temp\book1.xls"
'Const conRANGE = "A7:C13"
Set db = CurrentDb
Set objXL = New Excel.Application
Set rs = db.OpenRecordset("Customers", dbOpenSnapshot)
With objXL
.Visible = True
Set objWkb = .Workbooks.Open(conWKB_NAME)
On Error Resume Next
Set objSht = objWkb.Worksheets(conSHT_NAME)
'If Not Err.Number = 0 Then
For Index = 1 To 1
Set rs = db.OpenRecordset("Customers", dbOpenSnapshot)
objXL.Worksheets(Index).Range("A7,C13").CopyFromRecordset rs
Next
For Index = 2 To 2
Set rs2 = db.OpenRecordset("query2", dbOpenSnapshot)
objXL.Worksheets(Index).Range("k13,C13").CopyFromRecordset rs2
Next
For Index = 3 To 3
Set rs3 = db.OpenRecordset("tblprocedure", dbOpenSnapshot)
objXL.Worksheets(Index).Range("j7,C13").CopyFromRecordset rs3
Next
Err.Clear
On Error GoTo 0
End With
Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing
Set rs = Nothing
Set db = Nothing
End Sub
Currently, my code always updates: Const conWKB_NAME = "c:\temp\book1.xls".
How do I set Const conWKB_NAME = listbox1 value on form1?
Code:
Sub sCopyRSToNamedRange()
'Copy records to a named range
'on an existing worksheet on a
'workbook
'
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim db As Database
Dim rs As Recordset
Const conMAX_ROWS = 20000
Const conSHT_NAME = "Sheet1"
Const conWKB_NAME = "c:\temp\book1.xls"
'Const conRANGE = "A7:C13"
Set db = CurrentDb
Set objXL = New Excel.Application
Set rs = db.OpenRecordset("Customers", dbOpenSnapshot)
With objXL
.Visible = True
Set objWkb = .Workbooks.Open(conWKB_NAME)
On Error Resume Next
Set objSht = objWkb.Worksheets(conSHT_NAME)
'If Not Err.Number = 0 Then
For Index = 1 To 1
Set rs = db.OpenRecordset("Customers", dbOpenSnapshot)
objXL.Worksheets(Index).Range("A7,C13").CopyFromRecordset rs
Next
For Index = 2 To 2
Set rs2 = db.OpenRecordset("query2", dbOpenSnapshot)
objXL.Worksheets(Index).Range("k13,C13").CopyFromRecordset rs2
Next
For Index = 3 To 3
Set rs3 = db.OpenRecordset("tblprocedure", dbOpenSnapshot)
objXL.Worksheets(Index).Range("j7,C13").CopyFromRecordset rs3
Next
Err.Clear
On Error GoTo 0
End With
Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing
Set rs = Nothing
Set db = Nothing
End Sub