Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2006
    Posts
    559

    Unanswered: If populated, do not replace

    On my form, frmTask, it has a "user name" field, which is populated by the network login / db login name for that particular user when they FIRST click on "New Task" from the main menu screen.

    What I need is to have code on the "OnOpen" event of the form, frmTask, to check the 'User_Name' field on the form if there is data in it (like the user is opening a task that is already created to edit) then that data WILL NOT be replaced by the new user opening the form. However, if the field is empty, like the user clicked on "New Task" from the main menu, then it populates with that users name.

    Some thing like:

    If txtUserName.Text is NotNull

    Then populate with current user name (=fOSUserName).


    If txtUserName.Text is NotNull

    Then do nothing but open form with current data on that record

    Any ideas? or do I have it right?

  2. #2
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    It might be easier to do this from the button on the form (or event) which opens the frmTask.

    Private subOpenfrmTask()
    docmd.openform "frmTask"
    if forms!frmTask!User_Name = "" then forms!frmTask!User_Name = fOSUserName()
    end sub

    You can try doing it when the frmTask opens but you may have to issue a repaint or requery command first.

    I'm not sure if this will work but try it:
    Private Sub Form_Open(Cancel As Integer)

    me.repaint ' or me.requery
    if forms!frmTask!User_Name = "" then forms!frmTask!User_Name = fOSUserName()

    End Sub

    (I don't think it does work.)
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  3. #3
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    There might be another way you can do it when the frmTask opens but let me think about it. I've always done it from the calling event.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  4. #4
    Join Date
    Aug 2006
    Posts
    559
    Quote Originally Posted by pkstormy
    It might be easier to do this from the button on the form (or event) which opens the frmTask.

    Private subOpenfrmTask()
    docmd.openform "frmTask"
    if forms!frmTask!User_Name = "" then forms!frmTask!User_Name = fOSUserName()
    end sub

    You can try doing it when the frmTask opens but you may have to issue a repaint or requery command first.

    I'm not sure if this will work but try it:
    Private Sub Form_Open(Cancel As Integer)

    me.repaint ' or me.requery
    if forms!frmTask!User_Name = "" then forms!frmTask!User_Name = fOSUserName()

    End Sub

    (I don't think it does work.)
    Paul,

    Once again, I thank you for your responses.

    I have the form currently populating the user name (=fOSUserName) on the 'open form' event when they click the "New Task" command button from the main menu.

    What I'm trying to have happen is (it's all tied together):

    1. If the user clicks on the "Open Task" button from 'Main Menu', then types in an already created task, clicks 'Open', the user name is the same as the person who originally created the task
    2. On the "Search KeyWord", "Search User Name", "Search Contact", and "Search Orgainzation" when the user clicks on the task they wish, the original user name who created the task remains.

    I have a feeling that if I do not put some thing in there, it will constantly replace the user name of the field, txtUserName.Text, on the records? Am I correct? I may just be wrong though. The db I have doesn't do it, YET, but I haven't tried to edit anything that was already created.

  5. #5
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    If you want to put the code in the frmTask form, I think you might be better off putting the code in the Print - Save Current Task or Update Task buttons (right after the On error goto...statement).

    if forms!frmTask!User_Name = "" or isnull(Forms!frmTask!User_Name) then forms!frmTask!User_Name = fOSUserName()
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  6. #6
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    You definately DONT want it populating the User_Name in the on_open event (without criteria). Set the default value instead. Otherwise it won't matter who opens the task, their user_name will always be inserted into User_Name.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  7. #7
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    I couldn't get it to work nicely with criteria when the form opens but I'll try and look at it some more later.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  8. #8
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    Bryan,

    If you have the default value set to = fOSUserName(), you'll always get what you want unless the User_Name field can be edited or they can enter a task another way. Once you clear out all your task records and start new, there shouldn't be a time where User_Name isn't populated with the default set. You can also make this a required field in the table if you want to. Then they can't save the record if something unknown should happen.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  9. #9
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    As a last note, you can also set the default value for the User_Name in the TABLE design itself (i.e. =fOSUserName()!)
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  10. #10
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    You can also try the On_Current event. I don't think there's a type of event though which says "Do this once the form is open and the record is returned" on the opening form itself. OnCurrent is after the record is returned which may work for you but give it a try. Personally, I'd set the default value in the table design itself but you may have reasons not to.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •