PC Review


Reply
Thread Tools Rate Thread

Access vba to Excel

 
 
=?Utf-8?B?SmFuaXM=?=
Guest
Posts: n/a
 
      7th Nov 2007
I have asked this question on the Access programming list and have not gotten
a response. I think the question involves only Excel so perhaps you can help
me. The problem is on my EndRow variable. I see the count with the debugger
is 3 but the variable is still 0. Can you tell me why the variable doesn't
get stuffed? To understand what this program does is simple. The user
updates a Access record only one record at a time. The contents are input
from the 3 controls on the form into an Excel sheet. What is happening now
is the first row keeps getting the new update record but it writes over the
old record instead of being offset by one and adding the new record to the
next row. I think if you look you will see the Excel part and understand why
it doesn't offset? The first record is saved in the if clause and subsequent
records are saved in the Else clause. It should be really easy for Excel to
count down one row from the top but as I said in the debugger, the endRow
variable doesn't get a value.

thanking you in advance,

----------


Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists("c:\Test\Employees.xls") Then
Set appExcel = New Excel.Application
appExcel.Application.Visible = True
appExcel.DisplayAlerts = False
Set wbk = appExcel.Workbooks.Add
Set wks = appExcel.Worksheets(1)
wks.Name = "Emp"
wks.Activate



Cells(1, 1).Value = Me.Form.ID
Cells(1, 2).Value = Me.Form.FirstName
Cells(1, 3).Value = Me.Form.Salary

wbk.SaveAs ("C:\Test\Employees.xls")
wbk.Close
appExcel.Quit
Set dbs = Nothing
Set fso = Nothing

Else

Set appExcel = Excel.Application
appExcel.Visible = True
appExcel.DisplayAlerts = False

Set wbk = appExcel.Workbooks.Open("c:\Test\Employees.xls")

Set wks = appExcel.Worksheets("Emp")

wks.Activate
EndRow = wks.UsedRange.Count
' EndRow = Cells(Rows.Count, 1).End(xlUp).Select
Debug.Print EndRow
Cells(EndRow + 1, 1).Value = Me.Form.ID
Cells(EndRow + 1, 2).Value = Me.Form.FirstName
Cells(EndRow + 1, 3).Value = Me.Form.Salary
Debug.Print EndRow
wbk.SaveAs ("c:\Test\Employees.xls")
wbk.Close
appExcel.Quit
appExcel.Quit


End If

appExcel.DisplayAlerts = True
End Sub

 
Reply With Quote
 
 
 
 
JP
Guest
Posts: n/a
 
      7th Nov 2007
Can you explain what the EndRow variable is supposed to be capturing?
The row number of the last used row?


--JP


On Nov 6, 8:21 pm, Janis <Ja...@discussions.microsoft.com> wrote:

> EndRow = wks.UsedRange.Count


 
Reply With Quote
 
=?Utf-8?B?SmFuaXM=?=
Guest
Posts: n/a
 
      7th Nov 2007
I found the answer, sorry.

"Janis" wrote:

> I have asked this question on the Access programming list and have not gotten
> a response. I think the question involves only Excel so perhaps you can help
> me. The problem is on my EndRow variable. I see the count with the debugger
> is 3 but the variable is still 0. Can you tell me why the variable doesn't
> get stuffed? To understand what this program does is simple. The user
> updates a Access record only one record at a time. The contents are input
> from the 3 controls on the form into an Excel sheet. What is happening now
> is the first row keeps getting the new update record but it writes over the
> old record instead of being offset by one and adding the new record to the
> next row. I think if you look you will see the Excel part and understand why
> it doesn't offset? The first record is saved in the if clause and subsequent
> records are saved in the Else clause. It should be really easy for Excel to
> count down one row from the top but as I said in the debugger, the endRow
> variable doesn't get a value.
>
> thanking you in advance,
>
> ----------
>
>
> Set fso = CreateObject("Scripting.FileSystemObject")
> If Not fso.FileExists("c:\Test\Employees.xls") Then
> Set appExcel = New Excel.Application
> appExcel.Application.Visible = True
> appExcel.DisplayAlerts = False
> Set wbk = appExcel.Workbooks.Add
> Set wks = appExcel.Worksheets(1)
> wks.Name = "Emp"
> wks.Activate
>
>
>
> Cells(1, 1).Value = Me.Form.ID
> Cells(1, 2).Value = Me.Form.FirstName
> Cells(1, 3).Value = Me.Form.Salary
>
> wbk.SaveAs ("C:\Test\Employees.xls")
> wbk.Close
> appExcel.Quit
> Set dbs = Nothing
> Set fso = Nothing
>
> Else
>
> Set appExcel = Excel.Application
> appExcel.Visible = True
> appExcel.DisplayAlerts = False
>
> Set wbk = appExcel.Workbooks.Open("c:\Test\Employees.xls")
>
> Set wks = appExcel.Worksheets("Emp")
>
> wks.Activate
> EndRow = wks.UsedRange.Count
> ' EndRow = Cells(Rows.Count, 1).End(xlUp).Select
> Debug.Print EndRow
> Cells(EndRow + 1, 1).Value = Me.Form.ID
> Cells(EndRow + 1, 2).Value = Me.Form.FirstName
> Cells(EndRow + 1, 3).Value = Me.Form.Salary
> Debug.Print EndRow
> wbk.SaveAs ("c:\Test\Employees.xls")
> wbk.Close
> appExcel.Quit
> appExcel.Quit
>
>
> End If
>
> appExcel.DisplayAlerts = True
> End Sub
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel To Access: Transfer multiple rows from excel to access sam Microsoft Excel Programming 0 20th Jan 2010 09:51 PM
Access Speakers wanted - 2007 Access / Excel User Conferences Damon Longworth Microsoft Access 1 21st Aug 2006 05:01 AM
export access to excel. change access & update excel at same time =?Utf-8?B?ZmFzdGNhcg==?= Microsoft Excel Misc 0 24th Jun 2005 09:27 PM
Closing Excel files from Access and/or quitting Excel from Access Beverly Microsoft Access VBA Modules 1 11th Oct 2003 06:49 PM
Importing excel into access - want access to update data supplied from excel Deborah Microsoft Access External Data 1 30th Jul 2003 01:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:42 PM.