6 Replies - 698 Views - Last Post: 30 August 2012 - 08:44 PM Rate Topic: -----

#1 thefifthhjohn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-July 12

connection cannot be used

Posted 12 August 2012 - 04:33 AM

this is my code for the login form using ms access would someone please help me? Everytime I run the program, it usually says connection cannot be used.
Dim con As New ADODB.Command
Dim rs As New ADODB.Recordset

Private Sub cmdcancel_Click()
Unload Me
End Sub

Private Sub cmdLogin_Click()
Set rspassword = New ADODB.Recordset
rspassword.Open "Select * from table1"
If rspassword.RecordCount > 0 Then
    Do Until rspassword.EOF
        If txtUsername.Text = rspassword!UserName And txtPassword.Text = rspassword!Password Then
            'frmmain.L1.Caption = Text1.Text
            Set rspassword = Nothing
            Unload Me
                frmmain.Show
                        ctr = 0
                Exit Do
                Else
          rspassword.MoveNext
            ctr = ctr + 1

               
        End If
        Loop
        If ctr > 0 Then
         Set rspassword = Nothing
                MsgBox "Invalid!", vbExclamation
                Text1.Text = ""
                Text2.Text = ""
                Text1.SetFocus
        ctr = 0
    End If
        End If
End Sub


Attached image(s)

  • Attached Image
  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: connection cannot be used

#2 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,768
  • Joined: 26-March 09

Re: connection cannot be used

Posted 12 August 2012 - 05:21 AM

Where do you open the connection to the database?
Was This Post Helpful? 0
  • +
  • -

#3 thefifthhjohn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-July 12

Re: connection cannot be used

Posted 12 August 2012 - 05:53 AM

View Postmaj3091, on 12 August 2012 - 05:21 AM, said:

Where do you open the connection to the database?

Public rs As New ADODB.Recordset
Public con As New ADODB.Connection
Public Sub OpenCon()
    Set con = New ADODB.Connection
    con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " & App.Path & "\Database\enrollment.mdb"

Was This Post Helpful? 0
  • +
  • -

#4 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,768
  • Joined: 26-March 09

Re: connection cannot be used

Posted 12 August 2012 - 09:01 AM

Please don't PM me on open threads, keep things in the forum.

Does the connection open OK? Do you check it's actually open?
Was This Post Helpful? 0
  • +
  • -

#5 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: connection cannot be used

Posted 12 August 2012 - 05:40 PM

yes he opened it but where is he assigned the connection to the recordset
Was This Post Helpful? 0
  • +
  • -

#6 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,768
  • Joined: 26-March 09

Re: connection cannot be used

Posted 13 August 2012 - 12:12 AM

View Postthava, on 13 August 2012 - 01:40 AM, said:

yes he opened it but where is he assigned the connection to the recordset


Well spotted thava....you can tell I don't do much database work! :)

Have a look a the Recordset properties and methods.

You either need to set the ActiveConnection property, or pass it during your .Open call.
Was This Post Helpful? 0
  • +
  • -

#7 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: connection cannot be used

Posted 30 August 2012 - 08:44 PM

View Postmaj3091, on 13 August 2012 - 02:12 AM, said:

[
You either need to set the ActiveConnection property, or pass it during your .Open call.


Preferably the latter; the "stateless" vs. the "stateful" method.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1