Results 1 to 16 of 16

Thread: [RESOLVED] Updating fields in a database

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Resolved [RESOLVED] Updating fields in a database

    First, before i say anything really absurd, i'm just starting with visual basic, so i don't know that much about it.
    Ok so here is the deal, i'm creating a database in MS Access 2002, and i have several independent fields, that once you click a button are updated into a table. Something like i write "bob" in a text box, and when i press a button "bob" is added to my table.
    Now i managed to get this to work, but after developing a bit more the datbase it stoped working for some reason. Then i tested in a new batabase, with just a table with 2 fields, and a simple form, and it didn't worked either, so i guess there's something wrong with the code i'm using.
    I saw 2 different tutorials on how to do this, neither of them work, so i'll post both here:

    VB Code:
    1. Private Sub cmd1_Click()
    2.    Dim db As DAO.Database
    3.    Dim rst As DAO.Recordset
    4.  
    5.    Set db = CurrentDb
    6.    Set rst = db.OpenRecordset("Table1")
    7.  
    8.    rst.AddNew
    9.    rst("test").Value = txt1
    10.    rst.Update
    11. End Sub

    VB Code:
    1. Private Sub cmd1_Click()
    2.  
    3.  
    4.     Dim rst As ADODB.Recordset
    5.  
    6.     Set rst = New ADODB.Recordset
    7.    
    8.  
    9.     rst.Open "Table1", CurrentProject.Connection, adOpenStatic, adLockOptimistic
    10.    
    11.     If rst.Supports(adAddNew) Then
    12.         rst.AddNew
    13.         rst.Fields("test") = "txt1"
    14.         rst.Update
    15.     End If
    16.    
    17.  
    18.     rst.Close
    19.     Set rst = Nothing
    20.    
    21. End Sub

    So any ideas why it doesn't work (despite it being working before)?
    Last edited by BeKay; Apr 7th, 2006 at 11:48 AM.

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Updating fields in a database

    You need to open the recordset with a dynamic cursor if you want to write back to the DB. In both these cases you are using a Static cursor.

    If you change adOpenStatic to adOpenDynamic in the second procedure, you should be able to write to the table. You also don't need the IF statement.
    VB Code:
    1. Private Sub test()
    2.  
    3.  
    4.     Dim rst As ADODB.Recordset
    5.  
    6.     Set rst = New ADODB.Recordset
    7.    
    8.  
    9.     rst.Open "Table1", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
    10.    
    11.     rst.AddNew
    12.     rst.Fields("test") = "txt1"
    13.     rst.Update
    14.  
    15.     rst.Close
    16.     Set rst = Nothing
    17.    
    18. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    Re: Updating fields in a database

    Hi,

    Most programmers prefer using ADO instead of DAO if your project is vb6 (frontend).You may ignore the first code and instead follow DKenn'ys code.You may use DAO if your code is for MS access only (VBA).

    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Updating fields in a database

    Thanks for the answers, but it still isn't working, here is the exact code i'm using:

    VB Code:
    1. Option Compare Database
    2. Option Explicit
    3.  
    4. Private Sub cmdContinuar_Click()
    5.  
    6.     Dim rst As ADODB.Recordset
    7.  
    8.     Set rst = New ADODB.Recordset
    9.    
    10.     rst.Open "Semana", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
    11.    
    12.     rst.AddNew
    13.     rst.Fields("data_inicio") = txtDInicial
    14.     rst.Fields("data_fim") = txtDFinal
    15.     rst.Update
    16.  
    17.     rst.Close
    18.     Set rst = Nothing
    19.    
    20. End Sub

    "Semana" is the name of the table.
    "data_inicio" and "data_fim" are 2 date fields in the table. The table also has a increment(sp?) fiel named ID.
    "txtDInicial" and "txtDfinal" are two independent text box, wich are also formatted to have the date type.
    Reading the last post, my database is MS Access only, so does it make a big difference if i use ADO instead o DAO? If so how do i had a dynamic cursor using DAO?

  5. #5
    Junior Member
    Join Date
    Nov 2004
    Location
    Laredo, TX.
    Posts
    29

    Lightbulb Re: Updating fields in a database

    You are forgetting the most important part: Declaring the connection

    VB Code:
    1. Private cn As ADODB.Connection

    and: setting the connection before creating the recordset

    VB Code:
    1. Set cn = New ADODB.Connection
    2.     cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    3.     "Data Source= c:\database\access_database.mdb"
    4.     cn.Open

    the first chunk of code goes on the General section of your VB project and the second chunk of code goes on the form_load section.
    VB6 / SQL Server Express 2005.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Updating fields in a database

    Ok so 2 problems now, first i get a error when i open the form. My access version is in portuguese so i'll try to translate the best way i can:

    "The expression On Load that you introduced has a definition of the property of the event reported the following error: There was a problem while Microsoft Access was comunicating with the OLE server or the ActiveX control
    * the expression cannot result in the name of a macro, the name of a user defined function or [event procedure]
    * probablly there was a error evaluating the function, procedure or macro"

    Second thing i noticed that i need to point to the location of the database in my hard drive to make the conection, would this work if the database was located in a local server and was to be accessed from several computers in the network?

    Again here is my code now:

    VB Code:
    1. Option Compare Database
    2. Option Explicit
    3. Private cn As ADODB.Connection
    4.  
    5. Private Sub cmdContinuar_Click()
    6.  
    7.     Dim rst As ADODB.Recordset
    8.  
    9.     Set rst = New ADODB.Recordset
    10.    
    11.     rst.Open "Semana", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
    12.    
    13.     rst.AddNew
    14.     rst.Fields("data_inicio") = txtDInicial
    15.     rst.Fields("data_fim") = txtDFinal
    16.     rst.Update
    17.  
    18.     rst.Close
    19.     Set rst = Nothing
    20.    
    21. End Sub
    22.  
    23. Private Sub Form_Load()
    24.  
    25. Set cn = New ADODB.Connection
    26.  
    27.     cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    28.     "Data Source= C:\Documents and Settings\sergio\Desktop\bd1.mdb"
    29.     cn.Open
    30.    
    31. End Sub

    i also tried to put "Data Source= [C:\Documents and Settings\sergio\Desktop\bd1.mdb]" do the the path having a space, but it gave the same error.

    Thanks for all the replies so far

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Updating fields in a database

    sorry had to bump this, any one has any ideas?

  8. #8
    Frenzied Member d3gerald's Avatar
    Join Date
    Jan 2006
    Posts
    1,348

    Re: Updating fields in a database

    as what i have noticed in your sdo code above, you dont have an open connection. Perhaps you declared it somewher as public. make sure it is open and try your code again.
    On error goto Trap

    Trap:
    in case of emergency, drop the case...

    ****************************************
    If this post has been resolved. Please mark it as "Resolved" by going through the "Thread Tools" above and clicking on the "Mark Thread Resolved " option.
    if a post is helpful to you, Please Rate it by clicking on the Rate link right below the avatar

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Updating fields in a database

    CurrentProject.Connection is the open connection.

    Note, this is being done in Access VBA.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Updating fields in a database

    i've been reading posts and tutorials on the forum and still aint working
    i changed the code to the following:

    VB Code:
    1. Option Compare Database
    2. Option Explicit
    3. Private cn As ADODB.Connection
    4. Private rst As ADODB.Recordset
    5.  
    6. Private Sub cmdContinuar_Click()
    7.    
    8.     With rst
    9.         .AddNew
    10.         .Fields("data_inicio") = txtDInicial.Text
    11.         .Fields("data_fim") = txtDFinal.Text
    12.         .Update
    13.     End With
    14.  
    15.  
    16.    
    17. End Sub
    18.  
    19. Private Sub Form_Load()
    20.    
    21.     Set cn = New ADODB.Connection
    22.     cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    23.     "Data Source= \bd1.mdb"
    24.     cn.Open
    25.    
    26.     Set rst = New ADODB.Recordset
    27.     rst.Open "Semana", cn, adOpenDynamic, adLockOptimistic
    28.     rst.MoveFirst
    29.    
    30. End Sub
    31.  
    32. Private Sub Form_Unload(Cancel As Integer)
    33.  
    34.     cn.Close
    35.     rst.Close
    36.     Set rst = Nothing
    37.     Set cn = Nothing
    38.  
    39. End Sub

    still gives the same error, and doesn't work. I noticed that if i move the private variables on to the cmd click function, the error is gone, but it still doesn't do what i want. Any1 can give me any links about doing this with DAO?
    Also is there a way for me to test if the connection to the database/table is actually being done properlly?

  11. #11
    Addicted Member
    Join Date
    Mar 2006
    Posts
    155

    Re: Updating fields in a database

    VB Code:
    1. Option Explicit
    2. Private cn As New ADODB.Connection
    3. Private rst As New ADODB.RecordSet
    4.  
    5. Private Sub cmdContinuar_Click()
    6.    
    7.     With rst
    8.         .AddNew
    9.         .Fields("data_inicio") = txtDInicial.Text
    10.         .Fields("data_fim") = txtDFinal.Text
    11.         .Update
    12.     End With
    13. End Sub
    14.  
    15.  
    16. Private Sub Form_Load()
    17.  
    18.     Dim Connectionstring As String
    19.    
    20.     Set cn = New ADODB.Connection
    21.     Set rst = New ADODB.RecordSet
    22.     rst.CursorLocation = adUseClient
    23.    
    24.     'Make Sure your Database Path is correct
    25.     Connstring = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\bd1.mdb;Persist Security Info=False"
    26.        
    27.    
    28.     cn.Open Connstring
    29.    
    30.     'Assuming Semana is a Database Table form bd1.mdb
    31.    
    32.     rst.Open "Semana", cn, adOpenStatic, adLockOptimistic, adCmdTable
    33.    
    34.     txtDInicial.Text = rst.Fields("data_inicio")
    35.     txtDFinal = rst.Fields("data_fim")
    36.    
    37. End Sub
    38.  
    39. Private Sub Form_Unload(Cancel As Integer)
    40.  
    41.    cn.Close
    42.    rst.Close
    43.    Set rst = Nothing
    44.    Set cn = Nothing
    45.  
    46. End Sub
    47.  
    48. Private Sub RSTMoveNxt_Click()
    49. rst.MoveNext
    50. If rst.EOF Then rst.MoveLast
    51. txtDInicial.Text = rst.Fields("data_inicio")
    52. txtDFinal.Text = rst.Fields("data_fim")
    53. End Sub
    54.  
    55. Private Sub RstMovePrev_Click()
    56. rst.MovePrevious
    57. If rst.BOF Then rst.MoveFirst
    58. txtDInicial.Text = rst.Fields("data_inicio")
    59. txtDFinal.Text = rst.Fields("data_fim")
    60.  
    61. End Sub

    This shuould Work.
    Last edited by RobDog888; Apr 4th, 2006 at 03:26 AM. Reason: Added [vbcode] tags

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Updating fields in a database

    You may want to open your recordset with adOpenKeyset instead.
    VB Code:
    1. rst.Open "Semana", cn, adOpenKeyset, adLockOptimistic, adCmdTable
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Updating fields in a database

    Ok now using simon code i get a "variable not defined error" and it jumps to the App.path part :S

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Updating fields in a database

    Thats because in Access there is no App.Path. Use ...
    VB Code:
    1. Application.CurrentDb.Properties(0).Value
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Updating fields in a database

    stil not working

    i get the error i mentioned above whenever i load/unload the form, wich makes me think it has to do with the connection string:

    The expression On Load that you introduced has a definition of the property of the event reported the following error:
    There was a problem while Microsoft Access was comunicating with the OLE server or the ActiveX control
    * the expression cannot result in the name of a macro, the name of a user defined function or [event procedure]
    * probablly there was a error evaluating the function, procedure or macro
    what i've now:

    VB Code:
    1. Option Compare Database
    2. Option Explicit
    3. Private cn As New ADODB.Connection
    4. Private rst As New ADODB.Recordset
    5.  
    6. Private Sub cmdContinuar_Click()
    7.    
    8.     With rst
    9.         .AddNew
    10.         .Fields("data_inicio") = txtDInicial.Text
    11.         .Fields("data_fim") = txtDFinal.Text
    12.         .Update
    13.     End With
    14.    
    15. End Sub
    16.  
    17. Private Sub Form_Load()
    18.  
    19.     Dim Connstring As String
    20.    
    21.     Set cn = New ADODB.Connection
    22.     Set rst = New ADODB.Recordset
    23.     rst.CursorLocation = adUseClient
    24.    
    25.     Connstring = "Provider = Microsoft.Jet.OLEDB.4.0;
    26. Data Source=C:\Documents and Settings\sergio\Desktop\bd1.mdb; Persist Security Info=False" & _
    27.     "Application.CurrentDb.Properties(0).Value"
    28.        
    29.    
    30.     cn.Open Connstring
    31.  
    32.    
    33.     rst.Open "Semana", cn, adOpenKeyset, adLockOptimistic, adCmdTable
    34.    
    35.     txtDInicial.Text = rst.Fields("data_inicio")
    36.     txtDFinal = rst.Fields("data_fim")
    37.    
    38. End Sub
    39.  
    40. Private Sub Form_Unload(Cancel As Integer)
    41.  
    42.    cn.Close
    43.    rst.Close
    44.    Set rst = Nothing
    45.    Set cn = Nothing
    46.  
    47. End Sub
    48.  
    49. Private Sub RSTMoveNxt_Click()
    50.  
    51.     rst.MoveNext
    52.  
    53.     If rst.EOF Then rst.MoveLast
    54.  
    55.     txtDInicial.Text = rst.Fields("data_inicio")
    56.     txtDFinal.Text = rst.Fields("data_fim")
    57.  
    58. End Sub
    59.  
    60. Private Sub RstMovePrev_Click()
    61.  
    62.     rst.MovePrevious
    63.  
    64.     If rst.BOF Then rst.MoveFirst
    65.  
    66.     txtDInicial.Text = rst.Fields("data_inicio")
    67.     txtDFinal.Text = rst.Fields("data_fim")
    68.  
    69. End Sub

    i tought this would be easier :P

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Updating fields in a database

    Finally it's fixed, i forgot that I had changed the form properties so that it wouldn't allow addin new records.. sorry for wasting every1 time
    Still thanks for your help

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