Results 1 to 10 of 10
  1. #1
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    828

    Adding an ID in Website on Form?

    Password.zip

    Hi all,
    DB attached.
    On Form (frmOnLineAccess) when I enter a new name into field Search Institutions, it fire a Not in List, which it is suposed to, and When save it put an ID into the Website Field?
    Has something to do with the code below in RED but not sure why it is putting it in both the InstitutionID and the Website? Should only go in the InstitutionID in the corresponding table.

    Baffles me, maybe someone will see what the problem is on this. I think Valid helped me with this a long time ago and I never have been able to fix it?

    Code:
    
    Private Sub CboInstitutionSearch_NotInList(NewData As String, Response As Integer)
        Dim strSQL As String
        Dim maxID As Long
        On Error Resume Next
        
        If MsgBox("Institution is not in Current List, Would you Like to Add?", vbYesNo) = vbNo Then
            Me.Undo
            Me.CboInstitutionSearch = Null
            Response = acDataErrContinue
            Exit Sub
        End If
        strSQL = "Insert Into tblInstitution ([InstitutionName]) values ('" & NewData & "')"
        CurrentDb.Execute strSQL, dbFailOnError
        CurrentDb.Execute "INSERT INTO tblOnLineAccess(InstitutionID) VALUES(" & CurrentDb.OpenRecordset("SELECT @@IDENTITY").Fields(0) & ")"
        Response = acDataErrAdded
        Me.CboInstitutionSearch = Null
        Me.Requery
        Me.TxtWebsite.SetFocus
        maxID = DMax("InstitutionID", "tblInstitution")
        Me.RecordsetClone.FindFirst "Institution=" & maxi
        Me.Bookmark = Me.RecordsetClone.Bookmark
    End Sub
    Thank you


    Dave

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,927
    not verified the legitimacy of your code but you need to use a db object, not currentdb. Currentdb refreshes the instance every time it is referenced


    dim db as dao.database
    set db=currentdb
    strSQL = "Insert Into tblInstitution ([InstitutionName]) values ('" & NewData & "')"
    Db.Execute strSQL, dbFailOnError
    Db.Execute "INSERT INTO tblOnLineAccess(InstitutionID) VALUES(" & Db.OpenRecordset("SELECT @@IDENTITY").Fields(0) & ")"

  3. #3
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    828
    Thanks,
    I changed this and it still puts the InstutionID into the Website?

  4. #4
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Agree with using a variable db but that won't affect whether your code runs without error.

    I tested the code in A365 version 2510 Beta Channel 64-bit and it worked perfectly for me

    Attachment 53257
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  5. #5
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    828
    Thank you,
    I can actually put in the website and that is OK , Like You Did, but why when you add a new (Not in List) and hit save button does it put the ID in the Website If you dont enter a website?

  6. #6
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Ah I see - you omitted the most important point in your explanation

    This works for me:
    Code:
    CurrentDb.Execute "INSERT INTO tblOnLineAccess(InstitutionID, Website)" & _
            " VALUES(" & CurrentDb.OpenRecordset("SELECT @@IDENTITY").Fields(0) & ", '" & Nz(Me.TxtWebsite.Value, "") & "')"
    EDIT:
    Further testing showed there is a different problem. I'll try to look at it again later.
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  7. #7
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    828
    Quote Originally Posted by isladogs View Post
    Ah I see - you omitted the most important point in your explanation

    This works for me:
    Code:
    CurrentDb.Execute "INSERT INTO tblOnLineAccess(InstitutionID, Website)" & _
            " VALUES(" & CurrentDb.OpenRecordset("SELECT @@IDENTITY").Fields(0) & ", '" & Nz(Me.TxtWebsite.Value, "") & "')"
    EDIT:
    Further testing showed there is a different problem. I'll try to look at it again later.
    Thanks, Using the origional code I changed the "Me.TxtWebsite.SetFocus" to "Me.InstitutionName.SetFocus" and that got rid of the ID in the Website Field.
    Not sure why but it did work?
    Thanks again,
    Dave

  8. #8
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Glad you found a workaround.
    Doing that also prevents you clicking Save unless the Website field is populated which solves another problem!

    I suspect the code could be significantly simplified but as you have a solution, I'll leave it alone!
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  9. #9
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    430
    you may also try this:
    Attached Files Attached Files

  10. #10
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    828
    Quote Originally Posted by jojowhite View Post
    you may also try this:
    Thanks JoJowhite
    That works just beautifully
    Dave

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 0
    Last Post: 12-06-2016, 11:28 AM
  2. Replies: 2
    Last Post: 05-05-2015, 11:09 PM
  3. Importing Data from website when noit in tabular form
    By faodavid in forum Import/Export Data
    Replies: 3
    Last Post: 08-05-2014, 08:50 AM
  4. linking form to a website?
    By marklove in forum Access
    Replies: 4
    Last Post: 04-18-2013, 04:48 PM
  5. Retrieving data on my website
    By kattys in forum Access
    Replies: 1
    Last Post: 04-27-2010, 05:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums