Solved

VBA and SQL code to add information to two tables

Posted on 2014-02-27
3
915 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now