Results 1 to 4 of 4

Thread: how to connect VB6 to database

  1. #1
    Join Date
    Dec 2009
    Posts
    32

    how to connect VB6 to database

    I am trying to make project for final year of graduation. As per the golden rules of software development, I have created my database and set the primary key, and also set the foreign keys, User name and password.
    In short I have created the whole database with each and every requirement. Now the problem arises that I am unable to connect to database. The code through which I am trying to connect is given below.

    Dim dbconn As ADODB.Connection
    Dim dbrs As ADODB.Recordset
    Dim sqlstr As String

    Public Sub Form1_Load()
    Set Connect = CreateObject("ADODB.Connection")
    Connect.ConnectionString = "DSN=Xcellent Garment;UID=;PWD=;"
    Connect.Open
    Call combolist
    End Sub

    Public Sub combolist()

    'open database connection
    dbconn.Open ("DSN=Xcellent Garment")

    'customer
    sqlstr = "select * from Customer"
    dbrs.Open (sqlstr)

    If dbrs.RecordCount >= 1 Then
    dbrs.MoveFirst
    While Not dbrs.EOF
    Me.Combo1.AddItem (dbrs("CName").Value)
    'Me.Combo1.List (dbrs("CName").Value)
    dbrs.Move (1)
    Wend
    End If
    Combo1.Index = 1
    If dbrs.State = 1 Then
    dbrs.Close
    dbconn.Close

    End Sub

    If anybody can find solution for this problem please reply.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: how to connect VB6 to database

    First of all if you have mention user name and password in your database then you have to mention the same in your code given
    Just suppose if your user name: abc and Password: xyz then you have to change the code like this.
    Dim dbconn As new ADODB.Connection
    Dim dbrs As new ADODB.Recordset
    Dim sqlstr As String

    Public Sub Form1_Load()
    dbconn.open "DSN=Xcellent Garment;UID=abc ;PWD= xyz;"
    Call combolist
    End Sub

    Public Sub combolist()

    sqlstr = "select * from Customer"
    dbrs.Open sqlstr ,dbconn

    if dbrs.eof=true and dbrs.bof=true then exit sub

    dbrs.MoveFirst

    do until dbrs.eof
    Me.Combo1.AddItem (dbrs("CName").Value)
    dbrs.MoveNext
    loop

    Combo1.Index = 1
    If dbrs.State = 1 Then dbrs.Close
    if dbconn.state=1 then dbconn.close

    End Sub

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to connect VB6 to database

    You have to update vb6 version, with the latest service pack of visual basic. File name is vbrun60sp6.exe. After that you will be updated with all the latest mdac and other components, then you have to go project Reference, Add reference, Ado components etc. After that you need to code properly with the user name and password.
    dbconn.open "DSN=Xcellent Garment;UID=Your User Id ;PWD= Your password;"

    You have just take care of this snippet of code

    [B]Download[/B]

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: how to connect VB6 to database

    According to me there are many mistakes in the code given by you
    Try the following code with the latest service pack installed.

    Dim dbconn As new ADODB.Connection
    Dim dbrs As new ADODB.Recordset
    Dim sqlstr As String

    Public Sub Form1_Load()
    dbconn.open "DSN=Xcellent Garment;UID=”UserName”;PWD=”Password”;"
    Call combolist
    End Sub

    Public Sub combolist()

    sqlstr = "select * from Customer"

    if dbrs.state=1 then dbrs.close
    dbrs.cursorlocation=aduseclient

    dbrs.Open sqlstr ,dbconn
    dbrs.movelast
    msgbox dbrs.recordcount
    if dbrs.eof=true and dbrs.bof=true then exit sub ' insert break point here


    dbrs.MoveFirst

    do until dbrs.eof
    Me.Combo1.AddItem dbrs("CName")
    dbrs.MoveNext
    loop

    Combo1.Index = 1
    If dbrs.State = 1 Then dbrs.Close
    if dbconn.state=1 then dbconn.close

    End Sub
    I am sure you not going to get any other problem with your database connection.

Similar Threads

  1. How can i connect ā€˜Cā€™ language to a database
    By Mustafa k in forum Software Development
    Replies: 5
    Last Post: 05-01-2011, 10:55 AM
  2. How to use Python to connect DB2 database
    By Radames in forum Software Development
    Replies: 5
    Last Post: 04-03-2010, 01:40 AM
  3. How to connect ASP to different database
    By Elijah2010 in forum Software Development
    Replies: 5
    Last Post: 16-02-2010, 12:01 AM
  4. Connect Database to ASP.NET application
    By Ainsley in forum Software Development
    Replies: 4
    Last Post: 12-01-2010, 09:28 AM
  5. How to Connect Database and Retrieve it with the use of C#?
    By CheckMeNot in forum Software Development
    Replies: 3
    Last Post: 19-11-2009, 09:56 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,220,211.22330 seconds with 17 queries