Link to home
Start Free TrialLog in
Avatar of jammin140900
jammin140900

asked on

Setting criteria with an Insert Into Statement

Hey guys,

I forgot to ask this in my last post. I also need this insert into statement to check that a "WebsiteID" doesn't exist in the destination tables before inserting the current Website ID into them.

With the second insert (tblASGeneralWebsiteReg), I just fixed this by simple changing the property inside the table for Website ID to "No Duplicates". That fixes it. The first table however, has multiple entries of the same WebsiteID when it inserts as it inserts the Website ID against the 24 Site ID's it creates. Setting the table property for WebsiteID will only allow the first SiteID to show. But for each WebsiteID in tblASWebsiteSite, I need the 24 Site ID's with it.

Here's the code I got so far:

Private Sub Command20_Click()
On Error GoTo Err_Command20_Click

    Dim stDocName As String
    Dim stLinkCriteria As String
    Me.Refresh

 Dim strInsert As String
 
strInsert = "Insert into tblASWebsiteSite (WebsiteID, SiteID)" & vbCrLf
    strInsert = strInsert & "select tblWebsite.WebsiteID as WebsiteID, tblASSitesToSubmitTO.SiteID as SiteID" & vbCrLf
    strInsert = strInsert & "from tblWebsite, tblASSitesToSubmitTo" & vbCrLf
    strInsert = strInsert & "where tblWebsite.WebsiteID = " & Me!WebsiteID

    DoCmd.RunSQL strInsert

    strInsert = "Insert into tblASGeneralWebsiteReg ([WebsiteID])"
    strInsert = strInsert & " select tblWebsite.WebsiteID as WebsiteID"
    strInsert = strInsert & " from tblWebsite"
    strInsert = strInsert & " where tblWebsite.WebsiteID = " & Me!WebsiteID
   
  ''  DoCmd.SetWarnings False
    DoCmd.RunSQL strInsert
   '' DoCmd.SetWarnings True
    stDocName = "frmASRegistration"
   
    stLinkCriteria = "[WebsiteID]=" & Me![WebsiteID]
    DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_Command20_Click:
    Exit Sub

Err_Command20_Click:
    MsgBox Err.Description
    Resume Exit_Command20_Click
   
End Sub

Is it possible to put in criteria that checks for even one record of a WebsiteID in tblASWebsiteSite and the code above knows that it needn't do the insert again?
ASKER CERTIFIED SOLUTION
Avatar of MarkHurd
MarkHurd
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jammin140900
jammin140900

ASKER

Hi Mark,

I put in:

    strInsert = strInsert & "where tblWebsite.WebsiteID = " & Me!WebsiteID AND NOT EXIST (SELECT NULL AS NotNeeded FROM tblASWebsiteSite WHERE tblASWebsiteSite.WebsiteID = tblWebsite.WebsiteID AND tblASWebsiteSite.SiteID = tblASSitesToSubmitTO.SiteID)

but get a Syntax error?
You need to put the AND...) into the string. Try this:
 
strInsert = strInsert & "where tblWebsite.WebsiteID = " & Me!WebsiteID
   strInsert = strInsert & " AND NOT EXIST (SELECT NULL AS NotNeeded FROM tblASWebsiteSite WHERE tblASWebsiteSite.WebsiteID = tblWebsite.WebsiteID AND tblASWebsiteSite.SiteID = tblASSitesToSubmitTO.SiteID)"

Open in new window

Now I get an error. Here's the code:

 Dim strInsert As String
 
strInsert = "Insert into tblASWebsiteSite (WebsiteID, SiteID)" & vbCrLf
    strInsert = strInsert & "select tblWebsite.WebsiteID as WebsiteID, tblASSitesToSubmitTO.SiteID as SiteID" & vbCrLf
    strInsert = strInsert & "from tblWebsite, tblASSitesToSubmitTo" & vbCrLf
    strInsert = strInsert & "where tblWebsite.WebsiteID = " & Me!WebsiteID
strInsert = strInsert & "where tblWebsite.WebsiteID = " & Me!WebsiteID
   strInsert = strInsert & " AND NOT EXIST (SELECT NULL AS NotNeeded FROM tblASWebsiteSite WHERE tblASWebsiteSite.WebsiteID = tblWebsite.WebsiteID AND tblASWebsiteSite.SiteID = tblASSitesToSubmitTO.SiteID)"


    DoCmd.RunSQL strInsert

The attached file is a screen shot of the error. Any ideas please?

 Error-Message-VBA-210411.doc
SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You've duplicated the line:

    strInsert = strInsert & "where tblWebsite.WebsiteID = " & Me!WebsiteID
strInsert = strInsert & "where tblWebsite.WebsiteID = " & Me!WebsiteID

Only include it once.
Hey guys,

Made the changes as per your suggestions. Now get this error "Undefined function "EXPRESS" in expression"

Here's the changed code.

Dim strInsert As String
 
strInsert = "Insert into tblASWebsiteSite (WebsiteID, SiteID)"
    strInsert = strInsert & " select tblWebsite.WebsiteID as WebsiteID, tblASSitesToSubmitTO.SiteID as SiteID"
    strInsert = strInsert & " from tblWebsite, tblASSitesToSubmitTo"
    strInsert = strInsert & " where tblWebsite.WebsiteID = " & Me!WebsiteID
   strInsert = strInsert & " AND NOT EXIST (SELECT NULL AS NotNeeded FROM tblASWebsiteSite WHERE tblASWebsiteSite.WebsiteID = tblWebsite.WebsiteID AND tblASWebsiteSite.SiteID = tblASSitesToSubmitTO.SiteID)"

Debug.Print strInsert
    DoCmd.RunSQL strInsert

Any ideas please?
Sorry, that should read 'Undefined function "EXIST" in expression'
My mistake, it is EXISTS
Can you show the output of the Debug.Print statement?
Thanks guys. This is excellent!