If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Visual Basic > New bie playing with Access and Vb

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-18-10, 02:53
elpope elpope is offline
Registered User
 
Join Date: Apr 2010
Posts: 1
New bie playing with Access and Vb

Hi there , I'm new to Access and VB i do have so general knowledge in computer and networking but not in programming.

I have a working code that so far export an Access Query into a Excel Template.

I need some advice on how can i improve or clean this code.

Current known problems:

1. Every time i run the export it creates an EXCEL.EXE under Task Manager -> Process.
2. The way this code work is by grabbing an existing excel file with a nice format (Header.xls) add the current query information from (PriceList_STD)and saving it in "pricelist.xls".

The problem is that "pricelist.xls" keep the information from the previous export .

How can i clean the whole sheet before it adds the Query Data ?
-----------------------------------------------------------------------
Private Sub ExportLocal(ByVal strExcel As String, ByVal rsQ As Recordset)
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook

Dim objSht As Excel.Worksheet
Dim fRow As Integer
Const strSpace = " "
On Error GoTo errHandler
DoCmd.Hourglass True
Set objXL = New Excel.Application
Set objWkb = objXL.Workbooks.Open(strExcel)
Set objSht = objWkb.Worksheets("Price")
rsQ.MoveFirst
fRow = 7
objSht.Cells(1, 2).Value = "PERFUME " & Format(Date, "dddd, dd MMMM yyyy")
objSht.Cells(5, 2).Value = " PRICELIST "
While Not rsQ.EOF
objSht.Cells(fRow, 1).Value = rsQ!ItemNumber
objSht.Cells(fRow, 2).Value = rsQ!Description
objSht.Cells(fRow, 3).Value = rsQ![Boxes Of]
objSht.Cells(fRow, 4).Value = rsQ!Status
objSht.Cells(fRow, 5).Value = rsQ!StdPrice
rsQ.MoveNext
fRow = fRow + 1
Wend
rsQ.Close
Dim strExcelPath As String
strExcelPath = "E:\xls\"
Kill strExcelPath & "pricelist" & ".xls"
objWkb.SaveAs strExcelPath & "pricelist" & ".xls"

objXL.Visible = False

Set objSht = Nothing

objWkb.Close
Set objWkb = Nothing

objXL.Application.Quit
Set objXL = Nothing

exitHandler:
DoCmd.Hourglass False
Exit Sub
errHandler:
MsgBox Err.Description
Resume exitHandler
End Sub

Private Sub cmdExport_Click()
Dim rs As Recordset
Dim db As Database
Set db = CurrentDb
Set rs = db.OpenRecordset("PriceList_STD")
If rs.RecordCount > 0 Then
'Header.xls file must exist in the directory below
If IsFile("E:\xls\" & "Header.xls") Then
ExportLocal "E:\xls\" & "Header.xls", rs
Else
MsgBox "Local template is not found.", vbInformation, "Please select a valid Excel template."
End If
Else
MsgBox "No Records to export.", vbInformation, "Info."
End If
Set rs = Nothing
Set db = Nothing
End Sub
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On