Results 1 to 12 of 12

Thread: [RESOLVED] Need help connecting database to VB application

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    91

    Resolved [RESOLVED] Need help connecting database to VB application

    Can anyone please help me with connecting my databse to my VB application (not quite finished yet)?

    I have the connection string saved as a public constant (in module) called get_customer so i can reuse it, but I am getting errors when running the program.

    It works if i use Adodc1.connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & App.Path & "\FFES.mdb" and Adocdc1.recordsource = "SELECT * FROM Customer" but it still doesn't display in text boxes or labels

    If anyone could please have a look and tell me where i'm going wrong iwould be very grateful.
    I haven't done anything differently to another vb application i made about a year ago and that still works OK becuase I have compared them side-by-side.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Pakistan
    Posts
    436

    Re: Need help connecting database to VB application

    What error u r getting ?. Is error in connecting database or in data retreiving in recordset?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    91

    Re: Need help connecting database to VB application

    datasource name not found and no default driver specified

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Need help connecting database to VB application

    TRy using .OPEN on the ADODC after setting the connectionstirng.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    91

    Re: Need help connecting database to VB application

    Quote Originally Posted by techgnome
    TRy using .OPEN on the ADODC after setting the connectionstirng.

    -tg
    didn't work

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Need help connecting database to VB application

    Quote Originally Posted by AdRock952
    didn't work
    How did you use it?

    Post exactly what you ran as that should work just fine.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    91

    Re: Need help connecting database to VB application

    this is the code for the module
    Public Const FFES_CUSTOMER As String = "SELECT [Customer].Customer_ID, [Customer].Customer_Name, [Customer].Contact_Name, [Customer].Address1, [Customer].Address2, [Customer].County, [Customer].Postcode, [Customer].Telephone_No, [Customer].Email FROM [Customer]"
    Public get_customer As String
    'Public Const MyHelpFile As String = App.Path & "\help.htm"
    Sub Main()
    'path to the database (saves typing in each time connection to database is required)
    get_customer = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & App.Path & "\FFES.mdb"
    End Sub


    This is the code I am using in the form load
    Public Sub EditCustomer()
    Unload frmPrinted
    frmCustomer.Show
    frmCustomer.SetFocus
    ClearCustomer
    frmCustomer.fraList.Caption = "Edit Customer"
    frmCustomer.fraNew.Caption = "New Customer Details"
    frmCustomer.fraConfirm.Caption = "Confirm Customer Details"
    'Database connection
    frmCustomer.Adodc2.ConnectionString = get_customer
    frmCustomer.Adodc2.CommandType = adCmdUnknown
    frmCustomer.Adodc2.RecordSource = FFES_CUSTOMER
    Set frmCustomer.dgdCustomer.DataSource = frmCustomer.Adodc2
    frmCustomer.txtCustomerName.DataField = "Customer_Name"
    frmCustomer.txtContactName.DataField = "Contact_Name"
    frmCustomer.txtAddress1.DataField = "Address1"
    frmCustomer.txtAddress2.DataField = "Address2"
    frmCustomer.txtCounty.DataField = "County"
    frmCustomer.txtPostcode.DataField = "Postcode"
    frmCustomer.txtTelephone.DataField = "Telephone_No"
    frmCustomer.txtEmail.DataField = "Email"
    frmCustomer.Adodc2.Refresh

    End Sub


    I have done this before with no problems so I don't understand why it's not working

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Need help connecting database to VB application

    Set a reference to the Microsoft ActiveX Data Objects X.X Library (where X.X is the version on your machine
    VB Code:
    1. Option Explicit
    2.  
    3. PUblic ADOCn As ADODB.Connection
    4. Public ConnString As String
    5.  
    6.  
    7. Public Sub ConnectToDb
    8. ConnString = "Provider=Microsoft.Jet.OLEDB.4.0; _
    9.         Data Source=" & App.Path & "\FFES.mdb";" & _
    10.         "Persist Security Info=False"
    11.  
    12. Set ADOCn = New ADODB.Connection
    13. ADOCn.ConnectionString = ConnString
    14. ADOCn.Open ConnString
    15. End Sub
    16.  
    17. Private Sub Form_Load()
    18. ConnectToDb
    19. End Sub
    20.  
    21. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    22. 'close the database connection
    23. ADOCn.Close
    24. 'destroy it
    25. Set ADOCn = Nothing
    26. End Sub
    Your database is now open, and you have a public connection object (ADOCn) which can be used anywhere, on any form, in your project. It will remain open for as long as your program is running. When your program shuts downs, it will close the connection for you.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    91

    Re: Need help connecting database to VB application

    Thanks...it no longer crashes but i have another question.
    How do I add the database to a datagrid
    Do i still do

    VB Code:
    1. ADOCn.CommandType = adCmdUnknown
    2. ADOCn.RecordSource = "SELECT * FROM Customer" 'or whatever it is
    3. Set DataGrid1.DataSource = ADOCn
    4. ADOCn.Refresh

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Need help connecting database to VB application

    Quote Originally Posted by AdRock952
    Thanks...it no longer crashes but i have another question.
    How do I add the database to a datagrid
    Do i still do

    VB Code:
    1. ADOCn.CommandType = adCmdUnknown
    2. ADOCn.RecordSource = "SELECT * FROM Customer" 'or whatever it is
    3. Set DataGrid1.DataSource = ADOCn
    4. ADOCn.Refresh
    I haven't used data bound controls of any type for years and years, so I'm not completely sure. Did you try this to see if it still works, and if it doesn't, what it happening.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    91

    Re: Need help connecting database to VB application

    I got the connection working in the end.

    But do you know how I can get a record of the datagrid to display in labels when i click on a record on the datagrid.

    I can display a record in the labels but i want it so when i click on the right side of the datagrid the labels change

  12. #12
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Pakistan
    Posts
    436

    Re: [RESOLVED] Need help connecting database to VB application

    write following code on datagrid CLICK event

    for instance

    label1.caption = myRs!name

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width