Solved

using the excel sheet in a VB form

Posted on 2001-07-24
8
156 Views
Last Modified: 2012-06-21
I am trying to use the excel sheet in a vb form to keep the program clean.  When I added the sheet to the form it's default name is 'Sheet1'.  I want to be able to populate the sheet from a access DB and allow the user to make changes and then save data back to DB.  I'm having problems referencing the cells.  Can anyone show me the code on how to reference the 'Sheet1' so I can control the cells?
0
Comment
Question by:jayhogan
  • 4
  • 3
8 Comments
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6312923
Here is an example

Private Sub cmdButtonSum_Click()

   Dim Obj1 As Object
   Dim answer As Integer
       
        Set Obj1 = CreateObject("Excel.Application")
        Obj1.Visible = True
        Obj1.Workbooks.Add
        Obj1.Cells(1, 1).Value = textbox1.Text
        Obj1.Cells(2, 1).Value = textbox2.Text
        Obj1.Cells(3, 1).Formula = "=R1C1+R2C1"
        answer = Obj1.Cells(3, 1)
        MsgBox (answer)
        Obj1.Application.Quit
        Set Obj1 = Nothing



End Sub

0
 

Expert Comment

by:Alon_h
ID: 6313202
Hi jayhogan.

Try this :

Private Sub Command1_Click()
Dim xlApp As New Excel.Application
Dim xlWb As New Excel.Workbook
Dim xlSht As Excel.Sheets

   Set xlApp = CreateObject("Excel.Application")
   xlApp.Visible = True
   
   Set xlWb = xlApp.Workbooks.Add
   Set xlSht = xlWb.Sheets
   
   xlSht(1).Cells(1, 1) = "Test"
   MsgBox xlSht(1).Name
   
   xlApp.Quit
   Set xlSht = Nothing
   Set xlWb = Nothing
   Set xlApp = Nothing
   
End Sub
0
 

Author Comment

by:jayhogan
ID: 6313475
these 2 routines work but they still open excel outside of vb.  What I've added is under Project - Componets - Insertable Objects - and then Microsoft Excel Worksheet.  Adding this allows me to create a sheet on a form.  That's the one I'm trying to edit.
0
 
LVL 8

Accepted Solution

by:
Dave_Greene earned 100 total points
ID: 6313482
Just get rid of  "Set Obj1 = CreateObject("Excel.Application")"

Use Obj1 or whatever your control name is on the form  

Cheers
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:jayhogan
ID: 6314047
when i try to run

frmAddTags.Sheet1.Cells(1,1)

i get an error 'Method or Data Member not found
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6314056
looks like you are bypassing your excel object and coding right to the sheet level.  

frmAddTags.<EXCELControlNameHere>.Sheet1.Cells(1,1)
0
 

Author Comment

by:jayhogan
ID: 6314081
I figured it out. my line must read

frmAddTags.Sheet1.Object.Worksheets("Sheet1").Cells(1, 1) = "????"

I'm going to accept your answer because you did get me going towards the right direction. thanks
0
 

Author Comment

by:jayhogan
ID: 6314085
I figured it out. my line must read

frmAddTags.Sheet1.Object.Worksheets("Sheet1").Cells(1, 1) = "????"

I'm going to accept your answer because you did get me going towards the right direction. thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now