|
-
Jun 25th, 2000, 07:24 PM
#1
Thread Starter
New Member
What is the method to open an Excel spreadsheet and read in
the information within the cells. I don't want to open the
spreadsheet just read the data and manipulate the data as I
see fit within the VB application.
-
Jun 26th, 2000, 08:00 AM
#2
Frenzied Member
Problem is the spreadsheet file has a bunch of junk in it that Excel understands and isn't textual. So, unless you have the format of the file, you must rely on Excel to do the translating for you...
-
Jun 26th, 2000, 11:47 AM
#3
Guru
Do you want to manipulate the Excel sheet via Automation (Excel Object Model) or do you want to read the excel sheet via ADO?
-
Jun 26th, 2000, 03:16 PM
#4
Thread Starter
New Member
This is an example of all I can find but when run it doesn't do anything at all.
I read, somewhere and I can't remember, that when you open an external database you specify an option so that the sheet
does not open visibly.
I've not tried ADO with the sheet but just completed a project binding a db via ADO to a form using Access DB.
This is not what I want to achieve as it shouldn't be
opened to be shown. Like a text file and using the OPEN
command and reading the file's data but not seeing the file.
Public Sub LinkExcelSheet()
Dim dbsJet As Database
Dim rstSales As Recordset
Dim tdfExcelSheet As TableDef
Set dbsJet = OpenDatabase("C:\Samp.mdb")
' Create a TableDef object.
Set tdfExcelSheet = dbsJet.CreateTableDef _
("Linked Excel Worksheet")
' Set connection information.
tdfExcelSheet.Connect = "Excel 5.0;" _
& "Database=C:\Sales\Q1Sales.xls"
tdfExcelSheet.SourceTableName = "January Sales"
' Append the TableDef object to create a link.
dbsJet.TableDefs.Append tdfExcelSheet
' Open a Recordset object on the Microsoft Excel
' worksheet
Set rstSales = dbsJet.OpenRecordset _
("Linked Excel Worksheet")
End Sub
-
Jun 26th, 2000, 09:59 PM
#5
Guru
manipulate Excel via ADO
uses ADO 2.1 or higher (for Jet 4 provider)
Code:
Dim cn As New Connection
Dim rs As New Recordset
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Test.xls;Extended Properties=Excel 8.0;"
'excel sheet name here
'must enclose in brackets, sheet name followed by a dollar sign
rs.Open "Select * from [Sheet1$]", cn, adOpenKeyset, adLockOptimistic
'see value
MsgBox rs.Fields(0).Value
'change it
rs.Fields(0).Value = rs.Fields(0).Value & "3"
'update
rs.Update
'verify change
MsgBox rs.Fields(0).Value
'cleanup....
-
Aug 5th, 2000, 12:00 PM
#6
Lively Member
Cluniept, I tried your code with my excel database which has 4 columns. The first column contains text, the other columns contain numeric numbers. The rs.Fields().Value for all other columns show nulls instead of the actual numeric values. Could you tell me why it's doing that?
The statement MsgBox rs.Fields(2).Value show null instead of numeric value. But rs.Fields(0).Value shows the correct text.
-
Aug 6th, 2000, 02:37 PM
#7
Guru
You know, I had the same exact problem on another project where I had to do this, the value would come up as NULL when there was a value there. This is a bug with the driver
I believe we made the column a text column instead of a numeric and that fixed it...
If this doesn't fix it, let me know and I'll do some further research on how I fixed it....
Tom
-
Aug 6th, 2000, 07:24 PM
#8
Lively Member
Cluniept, Do you mean to save the column as text in the Excel database? How do you format the column to be text in Excel? Thanks!
-
Aug 7th, 2000, 01:33 AM
#9
New Member
Right click the cell in Excell and select Format Cells. Then
select Text from the listbox on the left.
Regards: VBJ
-
Aug 7th, 2000, 08:50 AM
#10
Frenzied Member
Do what VBj wrote as a macro, then cut-n-paste to your code, if you do it frequently.
-
Aug 7th, 2000, 09:08 AM
#11
Lively Member
Can I format the whole column instead of one cell?
Thanks!
-
Aug 7th, 2000, 09:23 AM
#12
Lively Member
Well, I made the columns to be text and it still shows nulls in VB. Does anyone know how to solve this? Thanks!
-
Aug 7th, 2000, 09:29 AM
#13
Frenzied Member
You won't experience the problem if you open an instance of an excel application and manipulate the cells directly. You will have to have excel on any machine that runs your app, if you choose to take this approach.
-
Aug 7th, 2000, 10:04 AM
#14
Lively Member
The problem is some machine don't have Access loaded. Any other suggestions? Thanks!
-
Aug 7th, 2000, 10:41 AM
#15
Frenzied Member
In access help, look up "TransferSpreadsheet Action". You can use this functionality to bring the data into an actual Access table.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|