473,320 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Allow "Got Focus" to Work Only Once

114 100+
Morning,

Here's my problem:
I have a field (ResFileLocation) on a form (frmResolution) that inserts a record-specific value (record ID, date stamp, etc) through vba when it has gained focus.

To help clarify, here is the basic code I am looking at:
Expand|Select|Wrap|Line Numbers
  1. Private Sub ResFileLocation_GotFocus()
  2.   Me!ResFileLocation = "Forms![frmResolution]!NoticeID & "R_" & Left(Date, 2) & Mid(Date, 4, 2) & Right(Date, 4)
  3. End Sub
However, this information should not be edited after it is inserted (by the user or the system) because it displays the file name of a PDF on a network drive.

So I tried setting the field Locked property to True after the GotFocus event, but the GotFocus event can still occur (i.e. the user can't edit, but the system still can); code below.
Expand|Select|Wrap|Line Numbers
  1. Private Sub ResFileLocation_GotFocus()
  2.   Me!ResFileLocation = "Forms![frmResolution]!NoticeID & "R_" & Left(Date, 2) & Mid(Date, 4, 2) & Right(Date, 4)
  3.   Me!ResFileLocation.Locked = True
  4. End Sub
Changing the Enabled property to False is out because I want the user to be able to select and copy the text for pasting in a browser.

So, is there a way to let the GotFocus event run only once, locking the text after that occurence?

Thank you for any advice or help!

martin
Jan 18 '08 #1
5 2212
sierra7
446 Expert 256MB
In the On_Current event of the form try writing something like;

Expand|Select|Wrap|Line Numbers
  1. If IsNull(Me!ResFileLocation) then
  2.      Me!ResFileLocation.Locked = False
  3. Else
  4.      Me!ResFileLocation.Locked = True
  5. End If
This should allow editing until the record has been saved.

If you wanted to force a 'save' immediately after ResFileLocation has been populated by your On-Focus event I would change YOUR line 3 to
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunCommand accCmdSaveRecord
I'm not sure I like using the OnFocus event to add data, but hey!

Hope this helps

S7
Jan 18 '08 #2
jaxjagfan
254 Expert 100+
Morning,

Here's my problem:
I have a field (ResFileLocation) on a form (frmResolution) that inserts a record-specific value (record ID, date stamp, etc) through vba when it has gained focus.

To help clarify, here is the basic code I am looking at:
Expand|Select|Wrap|Line Numbers
  1. Private Sub ResFileLocation_GotFocus()
  2.   Me!ResFileLocation = "Forms![frmResolution]!NoticeID & "R_" & Left(Date, 2) & Mid(Date, 4, 2) & Right(Date, 4)
  3. End Sub
However, this information should not be edited after it is inserted (by the user or the system) because it displays the file name of a PDF on a network drive.

So I tried setting the field Locked property to True after the GotFocus event, but the GotFocus event can still occur (i.e. the user can't edit, but the system still can); code below.
Expand|Select|Wrap|Line Numbers
  1. Private Sub ResFileLocation_GotFocus()
  2.   Me!ResFileLocation = "Forms![frmResolution]!NoticeID & "R_" & Left(Date, 2) & Mid(Date, 4, 2) & Right(Date, 4)
  3.   Me!ResFileLocation.Locked = True
  4. End Sub
Changing the Enabled property to False is out because I want the user to be able to select and copy the text for pasting in a browser.

So, is there a way to let the GotFocus event run only once, locking the text after that occurence?

Thank you for any advice or help!

martin
Check to see if it is locked when it gets focus. If it isn't, do your stuff - else move to next control.

Expand|Select|Wrap|Line Numbers
  1. Private Sub ResFileLocation_GotFocus()
  2. If Me!ResFileLocation.Locked = False Then
  3. Me!ResFileLocation = "Forms![frmResolution]!NoticeID & "R_" & Left(Date, 2) & Mid(Date, 4, 2) & Right(Date, 4)
  4. Me!ResFileLocation.Locked = True
  5. Else
  6. Me!YourNextControl.SetFocus
  7. End Sub
  8.  
Just a thought - What if there is an "Oops, I screwed up by the end-user?"

Since this control's value is set with code should it "ever" get focus? You don't need to SetFocus in order to set the value. I would trigger it to set the value on the AfterUpdate event of the first control the user enters a value. Set autotab to No so it doesn't get focus on OnCurrent or when user tabs through controls.
Jan 18 '08 #3
sierra7
446 Expert 256MB
Hi Jax

I follow your code and see how it works as a Data Entry system. Once ResFileLocation is visited it will be populated and locked WHILE ON THAT RECORD, but I don't understand how on future times the record is viewed (Browsed) that ResFileLocation will be Locked ?

I agree wholly with your last paragraph; this field should be populated as soon after the record is created as possible and not when the cursor happens to move there. Should it be editable at all ? I am assuming that [frmResolution] will definitely be open and NoticeID will have valid values.

Martin
I am not sure what will result by formulating the date code as you do. Would not Format(Date,"DDMMYYY") or Format(Date,"MMDDYYYY") if you prefer, be better? e.g.

Expand|Select|Wrap|Line Numbers
  1. Me!ResFileLocation = "Forms![frmResolution]!NoticeID & "R_" & Format(Date,"MMDDYYYY")
S7
Jan 19 '08 #4
martin DH
114 100+
Thank you both for your advice and suggestions. With these ideas I developed an alternative solution that fulfills all of the requirements.

First, yes frmResolution will be open. On this form, I locked the ResFileLocation textbox. I also added a command button that opens another form, frmLocation.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdLocation_Click()
  2. On Error GoTo Err_cmdLocation_Click
  3.     Dim stDocName As String
  4.     Dim stLinkCriteria As String
  5.     stDocName = "frmLocation"
  6.     DoCmd.OpenForm stDocName, , , stLinkCriteria
  7. Exit_cmdLocation_Click:
  8.     Exit Sub
  9. Err_cmdLocation_Click:
  10.     MsgBox Err.Description
  11.     Resume Exit_cmdLocation_Click
  12. End Sub
On frmLocation, I have four controls (3 textboxes and 1 command button).
txtExisting
txtFileLocation
txtFileName
cmdOK

txtExisting is locked and displays the data currently held in ResFileLocation on frmResolution
Expand|Select|Wrap|Line Numbers
  1. (=[Forms]![frmResolution]![ResFileLocation])
txtFileLocation is disabled but includes as a default the drive hierarchy on the network (will not change).

txtFileName includes as a default the name I was trying to include in the previous posts (with the date adjusted per S7's recommendation - thanks!). This textbox is unlocked and enabled so that the user can edit if necessary.

cmdOK saves txtFileLocation and txtFileName in ResFileLocation on frmResolution, then closes frmLocation.
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub cmdOK_Click()
  5.   Forms![frmResolution]!ResFileLocation = Me!txtLocation & Me!txtFileName
  6.   DoCmd.Close acForm, "frmLocation"
  7.  
  8. End Sub
  9.  
  10. Private Sub Form_Open(Cancel As Integer)
  11.   Me!txtLocation = "\\network\resolution\"
  12.   Me!txtFileName = Forms![frmResolution]!ClientID & "_NID" & Forms![frmResolution]!NoticeID & "R_" & Format(Date, "MMDDYYYY")
  13. End Sub
This way, the user doesn't need to know the exact file path where the associated file will be saved. The user can also manually edit/update the location if needed. Thank you for your help!

One last thing: I'm thinking it might be a good idea to have a message box pop up when the user selects cmdOK on frmLocation asking if sure of making change (yes, no option). Can anyone point me to a good reference on devising these through vba? I've never used them before.

martin
Feb 11 '08 #5
sierra7
446 Expert 256MB
One last thing: I'm thinking it might be a good idea to have a message box pop up when the user selects cmdOK on frmLocation asking if sure of making change (yes, no option). Can anyone point me to a good reference on devising these through vba? I've never used them before.

martin
Hi Martin

I'm glad we managed to help resolve your problem. It sounds as though you now have something more robust than relying on a On_Focus event.

I'm sorry about the delay in seeing this Message Box issue as I have been away for a week. As a general point it would have been better to raise it as a seperate thread, then other people would have seen it as a 'fresh' issue and would have responded by now.

The best advice I can give is to read the Access/VBA 'Help' topic and Example on this one because it is covered very well and there is no point repeating it here.

If any specific issues arise then raise them as new queries.

S7
Feb 16 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: TC | last post by:
Hello, I am using the script below to open a new window and once opened, redirect to that open window from the original window: private void btnNewPDFWindow_Click(object sender,...
2
by: Darin | last post by:
I have an html page (warehouse.htm) that has 2 frames (header.aspx and checkstock.aspx). When warehouse.htm opens, and the other 2 aspx pages load, I want the cursor to be sitting on the first...
0
by: Stefan De Schepper | last post by:
Dear NG, I created a gridlike usercontrol. When clicking on a cell a textbox (or other control, depending on the cell's editortype) is shown. When some other control gets the focus, the textbox...
2
by: Agnes | last post by:
Same application, When use the dataset expor to EXCEL, work very well. However, for some PC I will got the following error "DOES NOT SUPPORT A COLLECTION.(EXCEPTION FROM HRESULT :...
1
by: Robert Jones | last post by:
If my application doesn't have focus and someone clicks on the menu, the menu click is "lost" as the form gets focus, and the user then has to click on the menu a second time to actually activate...
0
by: woollymammoth | last post by:
I can't assign a MS SQL Server table record value to a simple VB variable, should be an easy thing. Sample SQL Server table has the data in the record as a char(30) string, the column for that record...
0
by: ytterbius | last post by:
Hello All, I'm trying to become more Google-friendly, which involves adding the "rel = "nofollow"" attribute to many of my outgoing links. I can't seem to find instructions on how to do this,...
8
by: freeskier | last post by:
I have been using the following code to cycle through a subform and disable all textboxes on a form. If a textbox on the form has the focus when this is run I get error "can't disable a control when...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.