Solved

imagebox array loses index, possibly  declare connectionstring globally

Posted on 2003-11-20
2
183 Views
Last Modified: 2013-12-25
i am using vb6sp5, and an access database to get strings for the pictures on the imageboxes

i have an array of imageboxes
on the click event, the index is called:

Private Sub imgPicture_Click(Index As Integer)

however i can click on say img1,then img2, then img3
and the indexes are called fine , namely, 1,2,3 respectively

then when i click on img1 for example again, the index is set to 0
this happens when i click on imageX, imageY and imageX again.
and i get an "unspecified error" the debugger stops on my connection string

 oconn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
               "Data Source=.\tapeus.mdb;" & _
               "User ID=admin;" & _
               "Password=;"
               
which is working fine, because i just connected to it twice.

maybe its the connection that does not like to be reconnected so much, how can i the connectionstring
public so i only need to connect once, i cant put it in general declaration
i am currently declaring it in every sub or function


0
Comment
Question by:jxharding
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 250 total points
ID: 9786099
Hi jxharding,

If you put the declaration in the declarations section of your form, you can set the connection in form_load. Alternatively you can do it in a module:

In a form:

'Declarations Section
Private oConn As ADODB.Connection

Private Sub Form_Load()
'Instantiate the object
  Set oConn = New ADODB.Connection
'Assign the connection string
  oConn.ConnectionString = "Your connection string"
'Open the connection
  oConn.Open
End Sub

Private Sub Form_Unload()
'Clean up and destroy connection
  If Not(oConn Is Nothing) Then 'the connection object has not been released
    If oConn.State <> adStateClosed Then 'the connection is open
      oConn.Close 'close the connection
    End If
    Set oConn = Nothing ' destroy the object
  End If
End Sub
 
In a module, you would simply have

Public oConn As ADODB.Connection

And then two subs which have the same code as for the form, however you need to then call these when your application starts and ends. A good way of doing this is by setting your startup object to Sub Main(). You then create SUB Main in your module:

Public Sub Main()
  'Initiate connection
  frmMyMainForm.Show vbModal 'pass control to the form
  'terminate connection
End Sub

Tim Cottee MCSD, MCDBA, CPIM
Brainbench MVP for Visual Basic
http://www.brainbench.com
0
 

Author Comment

by:jxharding
ID: 9786998
Hi Tim

heres my sub main module, which is set as start up:

(general declaration)
  Public oConn As ADODB.Connection

Public Sub Main()
   Set oConn = New ADODB.Connection
  oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
           "Data Source=.\tapeus.mdb;" & _
           "User ID=admin;" & _
           "Password=;"
  oConn.Open
  Form1.Show vbModal
End Sub

then it goes straight to form_load,which contains only
    Dim SQLQ As String
    SQLQ = "SELECT * FROM Products"
    rsProductsG.Open SQLQ, oConn, adOpenStatic, adLockBatchOptimistic, adCmdText

but when i reach this , oconn = nothing
and i get an "object required " message


0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

617 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