Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VBA and SQL code to add information to two tables

Posted on 2014-02-27
3
Medium Priority
?
1,019 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
[X]
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
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
Gustav Brock earned 2000 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 52

Expert Comment

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

/gustav
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

618 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