Results 1 to 38 of 38

Thread: Help, Unable to add Records into Database

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    17

    Help, Unable to add Records into Database

    Im using VB6 & Microsoft Access 2003.
    I cant seem to add records it says Error "3251" Current Recordset does not support updating. This may be limitation from the provider, or of the selected Locktype"

    Here is the code that's troubling me.

    Code:
     Dim rsDataCust As ADODB.Recordset
     Dim DBConnectionCust As ADODB.Connection
     
     
    Private Sub CmdMain_Click()
    Unload Me
    FrmMain.Show
    End Sub
    
    Private Sub CmdNext_Click()
            WriteRecord
             
            MsgBox "Patient successfully added !", vbOKOnly
            txtCustId.Text = " "
        TxtCustName.Text = " "
        TxtCustPhone.Text = " "
        TxtCustAdd.Text = " "
        TxtCustLID.Text = " "
        TxtCustIC.Text = " "
        TxtCustAge.Text = " "
         
    End Sub
    
    Private Sub Form_Load()
    Dim rsData As ADODB.Recordset
    
    Set DBConnectionCust = LoadDatabase(App.Path & "\LoginTest.mdb")
         
    Set rsDataCust = DBConnectionCust.Execute("SELECT * FROM CUSTOMER")
    
    End Sub
    
    Public Sub WriteRecord()
        With rsDataCust
        .AddNew
        .Fields("cust_id") = txtCustId
        .Fields("cust_name") = TxtCustName
        .Fields("cust_phone") = TxtCustPhone
        .Fields("cust_add") = TxtCustAdd
        .Fields("cust_license") = TxtCustLID
        .Fields("cust_ic") = TxtCustIC
        .Fields("cust_age") = TxtCustAge
        
          
             
        .Update
            
        End With
        End Sub
    And this is the module
    Code:
    Public DBConnectionCust As ADODB.Connection
    
    Public Function LoadDatabase_Cust(ByVal DatabaseName As String, ByVal cust_id As Integer, ByVal cust_name As String, ByVal cust_add As String, ByVal cust_phone As Integer, ByVal cust_ic As Integer, ByVal cust_license As Integer, ByVal cust_age As Integer) As ADODB.Connection
    
    Dim ConnectionData_Cust As ADODB.Connection
    Set ConnectionData_Cust = New ADODB.Connection
    
    ConnectionData_Cust.Provider = "Microsoft.Jet.OLEDB.4.0"
    ConnectionData_Cust.ConnectionString = "Data Source = " & DatabaseName
    ConnectionData_Cust.CursorLocation = adUseClient
    ConnectionData_Cust.Open , cust_id, cust_name, cust_add, cust_phone, cust_ic, cust_license, cust_age, conn, adOpenDynamic, adLockOptimistic
    
    Set LoadDatabase_Cust = ConnectionData
    
    End Function

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

    Re: Help, Unable to add Records into Database

    Moved from the CodeBank

  3. #3
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Help, Unable to add Records into Database

    I haven't used a record set for insertion or updation. I always use SQLs to do that. (May be older method, but at least I know what I'm doing).

    Code:
    Public Sub WriteRecord()
        Dim STRSQL As String
    
        STRSQL = "INSERT INTO Customer(Cust_ID,cust_name,cust_phone,cust_add,cust_license,cust_ic,cust_age) VALUES (" & _
        "'" & txtCustId.Text & "','" & TxtCustName.Text & "','" & TxtCustPhone.Text & _
        "','" & TxtCustAdd.Text & "','" & TxtCustLID.Text & "','" & TxtCustIC.Text & "'," & TxtCustAge.Text & ")"
        
        DBConnectionCust.Execute STRSQL
     
    End Sub
    this does the same (I think) as the your previous method.

    And you havent given the code for LoadDatabase , you have given LoadDatabase_Cust (It may not be important if its same as LoadDatabase_Cust)

    BTW, try changing cursor type to adUserServer
    like
    Code:
    ConnectionData_Cust.CursorLocation = adUseServer
    (this shoud be changed in LoadDatabase accordingly)

    IIF(Post.Rate > 0 , , )

  4. #4
    Addicted Member ThorSubak's Avatar
    Join Date
    Apr 2008
    Location
    Cebu
    Posts
    153

    Re: Help, Unable to add Records into Database

    instead:

    Set rsDataCust = DBConnectionCust.Execute("SELECT * FROM CUSTOMER")
    use this:
    rsDataCust .open "SELECT * FROM CUSTOMER",DBConnectionCust,adopenstatic,adopenoptimistic

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    17

    Re: Help, Unable to add Records into Database

    Sorry my bad!! I posted the wrong Module. Its suppose to be LoadDatabase_Cust


    Anyway ive been tinkering for awhile and now i got *Type MisMatch* from

    Code:
    Dim rsDataCust As DAO.Recordset
     Dim DBConnectionCust As ADODB.Connection
    --------------------------
      
    Private Sub CmdMain_Click()
    Unload Me
    FrmMain.Show
    End Sub
    --------------------
    
    
    Private Sub CmdNext_Click()
            WriteRecord
            rsDataCust.Update
            MsgBox "Patient successfully added !", vbOKOnly
            txtCustId.Text = " "
        TxtCustName.Text = " "
        TxtCustPhone.Text = " "
        TxtCustAdd.Text = " "
        TxtCustLID.Text = " "
        TxtCustIC.Text = " "
        TxtCustAge.Text = " "
         
    End Sub
    -------------------
    Private Sub Form_Load()
    
     
    Set DBConnectionCust = LoadDatabase_Cust(App.Path & "\LoginTest.mdb")
         
    
    Problem Code:
    1. Set rsDataCust = DBConnectionCust.Execute("SELECT * FROM CUSTOMER")
    ^ ^ ^ ^ This line here is the mismatch End Sub ------------------------- Public Sub WriteRecord() With rsDataCust .Edit !cust_id = txtCustId !cust_name = TxtCustName !cust_phone = TxtCustPhone !cust_add = TxtCustAdd !cust_license = TxtCustLID !cust_ic = TxtCustIC !cust_age = TxtCustAge End With End Sub
    The LoadDatabase_Cust Module
    Code:
    Public DBConnectionCust As ADODB.Connection
    
    Public Function LoadDatabase_Cust(ByVal DatabaseName As String, Optional ByVal cust_id As Integer, Optional ByVal cust_name As String, Optional ByVal cust_add As String, Optional ByVal cust_phone As String, Optional ByVal cust_ic As String, Optional ByVal cust_license As String, Optional ByVal cust_age As String) As ADODB.Connection
    
    Dim ConnectionData_Cust As ADODB.Connection
    Set ConnectionData_Cust = New ADODB.Connection
    
    ConnectionData_Cust.Provider = "Microsoft.Jet.OLEDB.4.0"
    ConnectionData_Cust.ConnectionString = "Data Source = " & DatabaseName
    ConnectionData_Cust.CursorLocation = adUseClient
    ConnectionData_Cust.Open = ("Select * FROM CUSTOMER")
    
    Set LoadDatabase_Cust = ConnectionData_Cust
    
    End Function
    For a better view ive included the forms etc in this rar.
    Username : Admin
    (Case sensitive) Password : Test
    Login Test.rar
    Last edited by CountPuchi; May 1st, 2008 at 02:33 PM. Reason: Added the Forms and etc

  6. #6
    Addicted Member ThorSubak's Avatar
    Join Date
    Apr 2008
    Location
    Cebu
    Posts
    153

    Re: Help, Unable to add Records into Database

    Did you try to put some breakpoints here?
    ConnectionData_Cust.Open = ("Select * FROM CUSTOMER")
    just take that point.
    Guess this help.

  7. #7
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Help, Unable to add Records into Database

    Code:
    ConnectionData_Cust.Open = ("Select * FROM CUSTOMER")
    I think this is wrong.
    open method requires a connection string as a parameter.
    to use a SELECT querry, first you have to open the connection to the database.
    Then use Connection.Execute, to execute any SQL querry (or any DB commands)

    it should be just ConnectionData_Cust.Open

    As for the Type mismatch, you are using a ADODB connection to populate a DAO recordset.
    ADODB connection returns a ADODB record set. change the Recordset type or connection type


    IIF(Post.Rate > 0 , , )

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    17

    Re: Help, Unable to add Records into Database

    ok i changed all the mismatched adodb with dao

    however, now it says
    Code:
    Set rsDataCust = DBConnectionCust.Execute("SELECT * FROM CUSTOMER")
    Where DBConnectionCust.Execute it highlighted execute as the missing function or variable

    The module
    Code:
    Public DBConnectionCust As DAO.Connection
    
    Public Function LoadDatabase_Cust(ByVal DatabaseName As String, Optional ByVal cust_id As String, Optional ByVal cust_name As String, Optional ByVal cust_add As String, Optional ByVal cust_phone As String, Optional ByVal cust_ic As String, Optional ByVal cust_license As String, Optional ByVal cust_age As String) As ADODB.Connection
    
    Dim ConnectionData_Cust As DAO.Connection
    Set ConnectionData_Cust = New DAO.Connection
    
    ConnectionData_Cust.Provider = "Microsoft.Jet.OLEDB.4.0"
    ConnectionData_Cust.ConnectionString = "Data Source = " & DatabaseName
    ConnectionData_Cust.CursorLocation = adUseServer
    ConnectionData_Cust.Open
    
    Set LoadDatabase_Cust = ConnectionData_Cust
    
    End Function
    So i guess the problem is not in the module right? but what kind of variable is missing now.

  9. #9
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Help, Unable to add Records into Database

    hmm, not sure why.
    Can you post the Full code like in Post #5,
    Cant be sure without seeing the declarations also.

    IIF(Post.Rate > 0 , , )

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    17

    Re: Help, Unable to add Records into Database

    Okay here's the full code for it

    Code:
    Dim rsDataCust As DAO.Recordset
     Dim DBConnectionCust As DAO.Connection
     
      
      
      
    Private Sub CmdMain_Click()
    Unload Me
    FrmMain.Show
    End Sub
    
    -------------------
    
    Private Sub CmdNext_Click()
            WriteRecord
            rsDataCust.Update
            MsgBox "Patient successfully added !", vbOKOnly
            txtCustId.Text = " "
        TxtCustName.Text = " "
        TxtCustPhone.Text = " "
        TxtCustAdd.Text = " "
        TxtCustLID.Text = " "
        TxtCustIC.Text = " "
        TxtCustAge.Text = " "
         
    End Sub
    ---------------------
    Private Sub Form_Load()
    
    
    Set DBConnectionCust = LoadDatabase_Cust(App.Path & "\LoginTest.mdb")
         
    Set rsDataCust = DBConnectionCust.Execute("SELECT * FROM CUSTOMER")  'DBConnectionCust,adopenstatic,adopenoptimistic
    
    
    
    End Sub
    -----------------------
    Public Sub WriteRecord()
        With rsDataCust
         
            .Edit
          
            !cust_id = txtCustId
            !cust_name = TxtCustName
            !cust_phone = TxtCustPhone
            !cust_add = TxtCustAdd
            !cust_license = TxtCustLID
            !cust_ic = TxtCustIC
            !cust_age = TxtCustAge
             
         
        End With
        End Sub

  11. #11
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Help, Unable to add Records into Database

    That is a big messed up between DAO and ADODB.
    Code:
    Public Function LoadDatabase_Cust(ByVal DatabaseName As String, _
                                      Optional ByVal cust_id As String, _
                                      Optional ByVal cust_name As String, _
                                      Optional ByVal cust_add As String, _
                                      Optional ByVal cust_phone As String, _
                                      Optional ByVal cust_ic As String, _
                                      Optional ByVal cust_license As String, _
                                      Optional ByVal cust_age As String) _
                                      As ADODB.Connection
    
        Dim ConnectionData_Cust As DAO.Connection
        Set ConnectionData_Cust = New DAO.Connection
        
        ConnectionData_Cust.Provider = "Microsoft.Jet.OLEDB.4.0"
        ConnectionData_Cust.ConnectionString = "Data Source = " & DatabaseName
        ConnectionData_Cust.CursorLocation = adUseServer
        ConnectionData_Cust.Open
        
        Set LoadDatabase_Cust = ConnectionData_Cust
    
    End Function
    All Optional parameters are never used in the function. Why are they there?

    Have you tried to compile the code? It may prompt errors at hundreds lines.

    A DAO.Connection Execute method does not return a DAO.Recordset so this line fails:
    Code:
    Set rsDataCust = DBConnectionCust.Execute("SELECT * FROM CUSTOMER")
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  12. #12
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Help, Unable to add Records into Database

    Oh dear.
    Thats it.
    I havent used DAO. Only ADO.

    Adding to anhn, it seems DAO has a method called OpenRecordset to return a record set. If you are going to use DAO, that might help.
    BTW, I always use rs.Open to open a record set.

    IIF(Post.Rate > 0 , , )

  13. #13
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Help, Unable to add Records into Database

    Yes. For this case, using DAO.Database
    dbs.OpenRecordset(..., dbOpenDynaset)
    is simpler.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    17

    Re: Help, Unable to add Records into Database

    Hmm..

    A DAO.Connection Execute method does not return a DAO.Recordset so this line fails:
    Code:

    Set rsDataCust = DBConnectionCust.Execute("SELECT * FROM CUSTOMER")
    So what your saying is, rsDataCust which is a DAO Record set gets nothing from
    ConnectionData_Cust As DAO.Connection??

    Yes i did compile the code but it only shows DBConnectionCust.Execute as the problem(Expected function or Variable)

    I removed all those parameters @ LoadDatabase_Cust.. ("Gomen... Im really trying hard to understand ")

  15. #15
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Help, Unable to add Records into Database

    BTW: That connection string looks suspect...

    look here http://www.connectionstrings.com

  16. #16
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Help, Unable to add Records into Database

    Quote Originally Posted by CountPuchi
    Yes i did compile the code but it only shows DBConnectionCust.Execute as the problem(Expected function or Variable)
    Yes, it shows you only one error at a time.
    If you comment out that line you will see another line, and so on...
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  17. #17
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Help, Unable to add Records into Database

    OK, one question
    You used ADO in the beginning, now you are using DAO
    Why?

    BTW, there is no property called Provider for DAO (3.51)


    IIF(Post.Rate > 0 , , )

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    17

    Re: Help, Unable to add Records into Database

    Well i guess my head is still too full trying to figure things out. While doing the coding i open the object library thingy full with codes then i saw the DAO.Edit line and so i thought i needed to use DAO instead of ADO. Oh well i scrap these and redo again.

    ill Update AsAp ^_^
    Last edited by CountPuchi; May 2nd, 2008 at 10:27 AM.

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    17

    Re: Help, Unable to add Records into Database

    Okay, after redoing everything and making it more simple (And compile check it)

    Code:
    Private Sub CmdMain_Click()
    Unload Me
    FrmMain.Show
    End Sub
    
    
    
    Private Sub CmdNext_Click()
            WriteRecord
            rsDataCust.Update
            MsgBox "Patient successfully added !", vbOKOnly
            txtCustId.Text = " "
        TxtCustName.Text = " "
        TxtCustPhone.Text = " "
        TxtCustAdd.Text = " "
        TxtCustLID.Text = " "
        TxtCustIC.Text = " "
        TxtCustAge.Text = " "
         
    End Sub
    
     
    
     
    
    Public Sub WriteRecord()
        With rsDataCust
         'With test
            .Edit
          
            !cust_id = txtCustId
            !cust_name = TxtCustName
            !cust_phone = TxtCustPhone
            !cust_add = TxtCustAdd
            !cust_license = TxtCustLID
            !cust_ic = TxtCustIC
            !cust_age = TxtCustAge
             
         
        End With
        End Sub
    
    Private Sub Form_Load()
        Dim LoadDataCust As Database
        Dim rsDataCust As ADODB.Recordset
        Dim ConnectionDataCust As ADODB.Connection
        Set ConnectionDataCust = New ADODB.Connection
        Set LoadDataCust = OpenDatabase(App.Path & "\LoginTest.Mdb")
        
        ConnectionDataCust.Provider = "Microsoft.Jet.OLEDB.4.0"
       
        Set rsDataCust = ConnectionDataCust.Execute("SELECT * FROM CUSTOMER")
    
            'Driver= (Microsoft Access Driver (*.mdb));Dbq=C:\Documents and Settings\CountPuchi\My Documents\MMU Notes & Work\Software Engine\Login Test\LoginTest.mdb;Uid=Admin;Pwd=;
    End Sub
    It says my database is not recognized. Im using Access 2003.. any thoughts on this?

  20. #20
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Help, Unable to add Records into Database

    wrong again
    OK you have figured out where it went wrong.
    So if you start from Post #1, and follow upto Post #7, I think you could get this sorted. Instead doing what I told in Post #7, you changed the Code something else too, so everything went wrong
    This is actually a separate problem.

    Try like that and see whether your original problem still exists.
    (I dont know why that is coming actually)
    IIF(Post.Rate > 0 , , )

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    17

    Re: Help, Unable to add Records into Database

    Thanks for the tip, Ive fixed the database unrecognized problem. However now i cant seem to add records (lol back to title.)

    Code:
    Public Sub WriteRecord()
        With rsDc
         'With test
            .NextRecordset.Fields("cust_name") = TxtCustName
            !cust_id = txtCustId
            !cust_name = TxtCustName
            !cust_phone = TxtCustPhone
            !cust_add = TxtCustAdd
            !cust_license = TxtCustLID
            !cust_ic = TxtCustIC
            !cust_age = TxtCustAge
             
         
        End With
        End Sub
    Thats not complete yet but as i was testing it says Object Variable & With Block Variable not set to all those lines above. Or is it just my way of trying to add records is simply plain wrong.

    Any good and simple ideas on how to add records?

  22. #22
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Help, Unable to add Records into Database

    Any good and simple ideas on how to add records?
    Check post #3

    as for the Object Variable & With Block Variable not set thing, if you havent set the rsDc to a valid object (ie: its Nothing) this would come.
    So before insertion, have you opened a recordset?
    Anyway, you have changed the rsDataCust to rsDC ;have you declared this (rsDC) as a global recordset?

    Arent you using Option Explicit

    IIF(Post.Rate > 0 , , )

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    17

    Re: Help, Unable to add Records into Database

    No im not using Option Explicit

    and yes RsDc is a global recordset

    So your saying a valid object

    etc

    Dim save As String
    save = rsDc


    ????

    im abit confused lol sry :P

  24. #24
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Help, Unable to add Records into Database

    Ok first thing.
    You must use Option Explicit. Its the first step in Error free code.
    So add Option Explicit to the start of ALL your modules.

    Next, well the example you gave is wrong.

    Thing is there are some kinds of variables, which needs a valid object to use them. For an example
    Dim rsDataCust As ADODB.Recordset will not make a valid variable.
    it names a varible, but it does not hold or point to a valid object. You need to create an object and then set this variable point to that.
    There are two ways to achieve this.
    1. Using NEW key word
    Code:
    Dim rsDataCust As New ADODB.Recordset
    2. point to another object returned by a method or created earlier, using Set keyword
    Code:
    Set LoadDatabase_Cust = ConnectionData
     Set ConnectionData_Cust = New ADODB.Connection
    without using one of these ways, a declared variable wont be a valid object.
    So , what you need to do is, before using the rsDC make it a valid object.
    ie: So, how would you do that?
    1. Use Conn.Execute() to get a valid Recordset object
    Code:
    Set rsDataCust = DBConnectionCust.Execute("SELECT * FROM CUSTOMER")
    2. Use New Keyword and rs.Open method to do that
    Code:
     Dim rsDataCust As New ADODB.Recordset
         rsDataCust.Open "SELECT * FROM CUSTOMER",DBConnectionCust,adopenstatic,adopenoptimistic
    so either of these ways make your record set a valid object.

    I dont know how much of these you'd uinderstand, so if you dont understand it clearly, say that, so i can give you a solution

    BTW, the code in post #1 , looks ok to me ,except that original problem of updation error.

    Last edited by zeezee; May 2nd, 2008 at 10:55 PM.
    IIF(Post.Rate > 0 , , )

  25. #25

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    17

    Re: Help, Unable to add Records into Database

    Thankyou thankyou!! I sorted the problem out..

    Ok for my next learning process,

    what does "Current Provider does not support returning multiple recordset from a single execution mean" ??

    and

    Operation is not allowed when the object is open

    Code:
     Dim ConnectionDataCust As ADODB.Connection
        Set ConnectionDataCust = New ADODB.Connection
        Set rsDataCust = New ADODB.Recordset
        
        
            
        
        ConnectionDataCust.Provider = "Microsoft.Jet.OLEDB.4.0"
        ConnectionDataCust.Open "DatBaseFyp.mdb", "", ""
        Set rsDataCust = ConnectionDataCust.Execute("SELECT * FROM CUSTOMER")
        rsDataCust.Open "SELECT * FROM CUSTOMER", ConnectionDataCust, asOpenStatic, asOpenOptimistic <---
    Last edited by CountPuchi; May 3rd, 2008 at 02:46 AM.

  26. #26
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Help, Unable to add Records into Database

    Where does that occur?
    may be, if you are using server side cursor (adUseServer), it wont allow you to open another record set while one is open. As the error says, current provider (or the DB driver you are using to connect) does not support that.
    so, if you are trying to open a second recordset while one is open, that might be the problem.
    But I'm not sure about that 100%

    IIF(Post.Rate > 0 , , )

  27. #27

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    17

    Re: Help, Unable to add Records into Database

    What about operation is not allowed when object is open??

    the code is on 25th post

  28. #28
    Addicted Member
    Join Date
    Mar 2008
    Posts
    143

    Re: Help, Unable to add Records into Database

    check if the table is open in ms access
    HTH

  29. #29

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    17

    Re: Help, Unable to add Records into Database

    if the table is open in msaccess?

    Msaccess is not even open.. so i guess something else??

  30. #30
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Help, Unable to add Records into Database

    No No. It has nothing to do with the Access.
    You cant use the Open for a Recordset (or even DB Connection) while its already Open. You have to Close before opening again.
    And this is true for when you close your app. You have to close all DB connections, Open Recordsets, Open Files , and destroy all objects before closing app.

    This is a code I use to close a Recordset
    Code:
    If Not rsDataCust Is Nothing Then
            If rsDataCust.State = adStateOpen Then
                rsDataCust.Close
            End If
            
            Set rsDataCust = Nothing
        End If
    IIF(Post.Rate > 0 , , )

  31. #31
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Help, Unable to add Records into Database

    Oh, well the code in Post#25 is wrong.
    You dont have to use both Conn.Execute or rs.Open, you have to use only one of them.

    eg:
    Code:
     Dim ConnectionDataCust As ADODB.Connection
        Set ConnectionDataCust = New ADODB.Connection
        Set rsDataCust = New ADODB.Recordset
        
        
        ConnectionDataCust.Provider = "Microsoft.Jet.OLEDB.4.0"
        ConnectionDataCust.Open "DatBaseFyp.mdb", "", ""
        rsDataCust.Open "SELECT * FROM CUSTOMER", ConnectionDataCust, asOpenStatic, asOpenOptimistic
    OR

    Code:
    Dim ConnectionDataCust As ADODB.Connection
        Set ConnectionDataCust = New ADODB.Connection
        
        ConnectionDataCust.Provider = "Microsoft.Jet.OLEDB.4.0"
        ConnectionDataCust.Open "DatBaseFyp.mdb", "", ""
        Set rsDataCust = ConnectionDataCust.Execute("SELECT * FROM CUSTOMER")

    got it?

    IIF(Post.Rate > 0 , , )

  32. #32

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    17

    Re: Help, Unable to add Records into Database

    Oooh... So basicly i can only use 1 of them to access the database but if i use both it will conflict with each other?

    However

    Code:
    Dim ConnectionDataCust As ADODB.Connection
        Set ConnectionDataCust = New ADODB.Connection
        
        ConnectionDataCust.Provider = "Microsoft.Jet.OLEDB.4.0"
        ConnectionDataCust.Open "DatBaseFyp.mdb", "", ""
        Set rsDataCust = ConnectionDataCust.Execute("SELECT * FROM CUSTOMER")
    if i use Con.Execute it will say the database does not support updating or change locktype (Which in turn to use the adLockOptimise i need to use the
    rsdatacust.open right??)

    but

    if i do it
    Code:
    ConnectionDataCust.Provider = "Microsoft.Jet.OLEDB.4.0"
        ConnectionDataCust.Open "DatBaseFyp.mdb", "", ""
        'Set rsDataCust = ConnectionDataCust.Execute("SELECT * FROM CUSTOMER")
        rsDataCust.Open "SELECT * FROM CUSTOMER", ConnectionDataCust, asOpenStatic, asOpenOptimistic, adLockOptimistic
    It says

    Arguments are wrong type, are out of acceptable range, or are in conflict of one another?

    is it because of rsDataCust.Open "SELECT * FROM CUSTOMER", asOpenStatic & asOpenOptimistic ?

  33. #33

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    17

    Re: Help, Unable to add Records into Database

    Yes i got my records working!!

    it was as i checked

    rsDataCust.Open "SELECT * FROM CUSTOMER", ConnectionDataCust, adOpenStatic, adLockOptimistic

    I Deleted adOpenOptimistic

  34. #34
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Help, Unable to add Records into Database

    Oooh... So basicly i can only use 1 of them to access the database but if i use both it will conflict with each other?
    both are fine, you can use both with conjunction, but you cant open a Recordset which is already open. So in the previous code,
    Code:
    rsDataCust.Open "SELECT * FROM CUSTOMER", ConnectionDataCust, asOpenStatic, asOpenOptimistic <---
    line tries to open rsDataCust again , which is already open from the line above.
    Thats why it says you cant open that.

    the correct open code would look like this

    Code:
    rsDataCust.Open "SELECT * FROM CUSTOMER", ConnectionDataCust, adOpenStatic, adLockReadOnly
    When you type this in the Code window, usually the correct constants comes automatically.

    IIF(Post.Rate > 0 , , )

  35. #35

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    17

    Re: Help, Unable to add Records into Database

    Owh so thats why.. nevertheless i got it working!! Thank you all!!!!!

    On to the next topic.

    The customer ID is the primary key in my database, So in my form i want it to show there automatically what is the last ID that's in the record so i did my code like this

    Code:
     If rsDataCust.RecordCount > 0 Then
            rsDataCust.MoveFirst
            
            Do Until rsDataCust.EOF Or rsDataCust.BOF
                 
                Me.txtCustID.text = rsDataCust.Fields("Cust_ID")
            Loop
        End If
    is this correct??

  36. #36
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Help, Unable to add Records into Database

    It is correct.
    However, IF your PK is Numeric (integer) and its incremental or auto number, there is an easier way (provided no customers are deleted also)

    Code:
    SELECT MAX(Cust_ID) FROM Customer
    or even , without a loop, just use a rsDataCust.MoveLast, and get the CustomerID.

    Is your Original Problem solved, Unable to Adding records?

    IIF(Post.Rate > 0 , , )

  37. #37

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    17

    Re: Help, Unable to add Records into Database

    Yes the records are adding in smoothly like in post #33

    Thanks to all for helping and especially to you zeezee for sticking with me ^_^

    Now i can add all the forms i want and insert edit delete etc records ^_^

    SELECT MAX(Cust_ID) FROM Customer

    MAX = the last record right?
    so

    Code:
    If rsDataCust.RecordCount > 0 Then
            rsDataCust.MoveFirst
            
            Do Until rsDataCust.EOF Or rsDataCust.BOF
                 
                Me.txtCustID.text = rsDataCust.Open "SELECT MAX Cust_ID FROM Customer
            Loop
        End If
    Is this how the code looks like?

  38. #38
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Help, Unable to add Records into Database

    no it isn't.
    to use the MAX thing, you need another RecordSet, ie another query.

    its better to have a function for that
    eg:

    Code:
    Private Function GetMaxCustID() As String
        Dim RstMaxID As New ADODB.Recordset
        
        RstMaxID.Open "SELECT MAX(Cust_ID) FROM Customer", ConnectionDataCust, adOpenStatic, adLockReadOnly
        If Not RstMaxID.EOF Then
        
            RstMaxID.MoveFirst
            ' if you need to make it next id, do it here
            ' eg :
            ' GetMaxCustID = CLng(Val(RstMaxID.Fields("Cust_ID"))) + 1
            
            
            GetMaxCustID = RstMaxID.Fields("Cust_ID")
                   
        Else
            ' or set to initial ID
            GetMaxCustID = ""
        End If
    End Function
    So, if your Original Problem is solved, then you can mark the Thread Resolved
    Because it's forum Policy to keep one thread for one problem.
    If you have any other problems, you may ask them in separate threads .

    IIF(Post.Rate > 0 , , )

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