Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

VBA and SQL code to add information to two tables

Posted on 2014-02-27
3
943 Views
1 Endorsement
Last Modified: 2014-03-01
I have a piece of code that is supposed to take information entered into a text box, check a table to see if it has been added to that table yet, if not, add it, if so, skip that step. Then the item should be chosen from the list (activity table) should be added to another table (StaffActivities) with information that is in the form.

I have a piece of code that a friend helped me with, which worked in another database. I am having errors with the very last DoCmd statement. It is giving me errors already and I haven't been able to test the code.

I am attaching the database so you can see it in context. The form is frm_act1TO and the button I have written the code behind is btnTest.

Here is the code:

Private Sub btnTest_Click()
Dim strSql As String
Dim db As Database
Dim rst As DAO.Recordset
Dim LngItem As Long


Dim rowc As Interger

With Me.txtactdesc
    For rowc = 0 To .ListCount - 1
    
    strSql = "Select * From Activities Where ActDesc = '" & .Column(0, rowc) & "'"
    
Set rst = db.OpenRecordset(strSql)

    If (rst.BOF And rst.EOF) Then
    
    DoCmd.RunSQL "Insert Into Activities (actdesc) Values ('" & Column(0, rowc) & "'"
    
rst.Close

End If

strSql = "Select ActID from Activities Where ActDesc = '" & .Column(0, rowc) & "'"

Set rst = db.OpenRecordset(strSql)

DoCmd.RunSQL "Insert Into StaffActivities (ActDate, ActID, STOID, TOID)"
                Values "(actDate, '"rst.Fields(0).value &"'), cmbsto, cmbTO"
                
                
           Next rowc
           
End With
                
                  
    
End Sub

Open in new window


The other thing I have done is not include the StaffID field in the list of fields from the StaffActivity table. I think that I may take that field out, as it seems redundant and may be very difficult for me to get programed into this whole thing.

I am also concerned about using SQL code in this. I have been going through and switching a lot of the code behind these forms to strickly VBA. I have heard that it isn't good to use both or switch between the two.

Thank you for any help or advice you can give!
For-Use-on-Utter-Access.accdb
1
Comment
Question by:Megin
  • 2
3 Comments
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 39894258
Skip all the SQL stuff and use DAO.
This is how:
Private Sub btnTest_Click()

    Dim db      As DAO.Database
    Dim rst     As DAO.Recordset
    Dim strSql  As String
    Dim varDesc As Variant
    Dim lngID   As Long

    varDesc = Me.txtactdesc.Value
    
    strSql = "Select * From Activities Where ActDesc = '" & varDesc & "'"
    
    Set db = CurrentDb
    Set rst = db.OpenRecordset(strSql)
    
    If rst.RecordCount = 0 Then
        rst.AddNew
            rst!ActDesc.Value = varDesc
            rst!ActTypeID.Value = Me!lsttype
            rst!STOID.Value = Me!cmbSTO.Value
            rst!TOID.Value = Me!cmbTO.Value
        rst.Update
        rst.Bookmark = rst.LastModified
    End If
    lngID = rst!ActID.Value

    strSql = "Select Top 1 * From StaffActivities"
    Set rst = db.OpenRecordset(strSql)
    
    rst.AddNew
        rst!ActDate.Value = Nz(Me!ActDate.Value, Date)
        rst!ActID.Value = lngID
        rst!STOID.Value = Me!cmbSTO.Value
        rst!TOID.Value = Me!cmbTO.Value
        rst!StaffID.Value = Me!cmbStaff.Value
    rst.Update
    rst.Close
    
    Set rst = Nothing
    Set db = Nothing
                
End Sub

Open in new window

You will need some error handling for missing entries like staff.

/gustav
NotFromUtterAccess.accdb
0
 

Author Closing Comment

by:Megin
ID: 39896418
Holy  Cow!!!!  This totally works!!!!!

I don't know how to thank you enough. I have been working on this forever. I am soooo excited. I wish I could give you 10,000 points for answering this!
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39896865
That's very kind of you. Enjoy!

/gustav
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question