Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 7th, 2010, 01:34 AM
Registered User
 
Join Date: Aug 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default VB 6.0 and SQL Connection Problem

Hello,

I am a beginner in coding with VB 6.0. While i tried to learn how to fetch data from Database into a UI form by SQL Queries, i tried the following code with a text file (myTable.txt) being used as data source which is kept under the path C:\DB.

Private Sub Command1_Click()
Dim connectionText As New ADODB.Connection
Dim recordSetObj As New ADODB.Recordset
Dim path As String

path = "C:\DB\"

connectionText.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" _
& path & ";Extended Properties='text;HDR=YES;FMT=Delimited'"


recordSetObj.Open "Select * From myTable.txt WHERE Year=1977;", _
connectionText, adOpenStatic, adLockReadOnly, adCmdText
Do While Not recordSetObj.EOF
MsgBox recordSetObj(0) & ", " & recordSetObj(1) & ", " & recordSetObj(2)
recordSetObj.MoveNext
Loop

End Sub

I got the above code from the link given below
http://www.ehow.com/way_5568882_visu...-tutorial.html

This code runs perfectly OK when am using text file as database and it fetches the required data into message box too. But when I am using excel as datasource it gives me an error saying the database is read only. I modify the code as below to look up the excel file though.

Private Sub Command1_Click()
Dim connectionText As New ADODB.Connection
Dim recordSetObj As New ADODB.Recordset
Dim path As String

path = "C:\DB\"

connectionText.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" _
& path & ";Extended Properties='text;HDR=YES;FMT=Delimited'"


recordSetObj.Open "Select * From myTable.xlsx WHERE Year=1977;", _
connectionText, adOpenStatic, adLockReadOnly, adCmdText
Do While Not recordSetObj.EOF
MsgBox recordSetObj(0) & ", " & recordSetObj(1) & ", " & recordSetObj(2)
recordSetObj.MoveNext
Loop

End Sub

Can i get a help about why this is happening and how to resolve this?
 
Old August 8th, 2010, 03:48 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

That's not how you connect to and read from Excel. You do *NOT* treat Excel as a text file (because it isn't).

Look here:
http://www.carlprothman.net/Default....orMicrosoftJet

Scroll down to the section titled "You can also open an Excel Spreadsheet using the JET OLE DB Provider"

So you will want something like
Code:
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
           "Data Source=" &* path & "\myTable.xlsx;" & _
           "Extended Properties=""Excel 10.0;HDR=Yes""" 
recordSetObj.Open "Select * From [sheetname$] WHERE [Year]=1977;", oConn
Do *NOT* use adOpenStatic unless you really need to. And I'm not sure it will work with Excel, anywyay.

I am *guessing* that you need "Excel 10.0" as the Extended Properties value. Have not worked with ".xslx" (only with ".xls") so I'm not sure. Play with it.

******

Note that you *MUST* put [...] around the sheetname and must append the $ to the end of the name.

Also, note that YEAR is a keyword in JET SQL so you must put the [...] around it, as well.

Last edited by Old Pedant; August 8th, 2010 at 04:07 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
VB 2005 Crystal Report Connection with SQL 2000 blackspear BOOK: Professional Crystal Reports for VS.NET 0 December 3rd, 2008 09:49 AM
VB App - Connection to SQL timsweet VB How-To 1 July 19th, 2006 05:12 PM
sql server connection problem in msde oldbig89 SQL Server 2000 1 February 8th, 2006 05:46 PM
SQL server connection problem rajanikrishna Classic ASP Databases 1 February 18th, 2004 08:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.