Results 1 to 12 of 12

Thread: [RESOLVED] Access 2003 - Insert Record

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Resolved [RESOLVED] Access 2003 - Insert Record

    Hi All,

    I can trying to insert a record from a form using a button's onclick event but have run into some problems.

    Problem 1:

    When the onclick event is fired, I am prompted with an error message stating that the access database file can not be found in my documents. How do I over come that problem?

    Problem 2:

    It's not really a problem but more like a I have forgotten the syntax. When creating the insert syntax, I can't remember how to specify the textboxes as values. You can see from my code below.

    Code:
    Private Sub btnCreateEmp_Click()
    
    Dim cn As ADODB.Connection
    Dim cmd As ADODB.Command
    Dim sSQL As String
    Dim strConString As String
    
    Set cn = New ADODB.Connection
    Set cmd = New ADODB.Command
    
    strConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Client Details.MDB; Persist Security Info=False;Mode=Read|Write"
    
    cn.ConnectionString = strConString
    cn.Open
    
    sSQL = "INSERT INTO EmployeeDetails ([FirstName]) VALUES ('Me!FNAME')"
    
    With cmd
    .ActiveConnection = cn
    .CommandText = sSQL
    .CommandType = adCmdUnknown
    .Execute
    End With
    
    Set cmd = Nothing
    cn.Close
    Set cn = Nothing
    
    End Sub

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

    Re: Access 2003 - Insert Record

    You didnt specify the complete path so it doesnt know where its located.

    Data Source=Client Details.MDB;

    Should be ...

    Data Source=" & App.Path & "\Client Details.MDB;

    If the database is in the same folder as the exe.
    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

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Access 2003 - Insert Record

    For problem 2, see the article about it in the SQL section of our Database Development FAQs/Tutorials (at the top of the Database Development forum)

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Question Re: Access 2003 - Insert Record

    Thanks RobDog888 and si_the_geek for your replies. I can now insert records using a RecordSet but I am still having problems with the connection string.

    Working Connection String:

    Code:
    conConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ClientDetails.MDB; Persist Security Info=False;Mode=Read|Write"
    Non-Working Connection String

    Code:
    conConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & App.Path & "\ClientDetails.MDB;Mode=Read|Write
    When using the above connection string, a Run-Time error '424': Object Required is returned

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Access 2003 - Insert Record

    OK, well in that case I guess you aren't using Classic VB.

    Are you doing this inside Access itself (thus VBA)?

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

    Re: Access 2003 - Insert Record

    Sounds like it may be in Access but trying to connect to a secondary access database.

    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Re: Access 2003 - Insert Record

    Are you doing this inside Access itself (thus VBA)?
    Everything is being written / designed within Access

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

    Re: Access 2003 - Insert Record

    Thread Moved from the VB 6 Forum to the Office Dev. Forum

    Ok since its not VB 6 the App.Path wont work as its not supported in VBA.
    Are you trying to connect to a separate database or the same one the VBA code is in?
    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

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

    Re: Access 2003 - Insert Record

    I think he's just making the code within the database.....

    Greg
    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...

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

    Re: Access 2003 - Insert Record

    Yes but if hes trying to connect to the same database then thats not needed as you can simply do a ...

    Application.CurrentDb.Connection
    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

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Re: Access 2003 - Insert Record

    Quote Originally Posted by RobDog888
    Yes but if hes trying to connect to the same database then thats not needed as you can simply do a ...

    Application.CurrentDb.Connection
    The code is within the database. I now have it working using:

    Code:
    rs.Open stSQL, CurrentProject.Connection
    Cheers to everyone that helped

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

    Re: [RESOLVED] Access 2003 - Insert Record

    You should use the CurrentDb instead of the CurrentProject.

    You can use the CurrentProject property to access the CurrentProject object and its related collections, properties, and methods.
    The CurrentDb method returns an object variable of type Database that represents the database currently open in the Microsoft Access window.
    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

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