Excel 2010 - Userform - VBA How to stop 'Job No' from duplicating itself on next empty row


Hi there
Thank you in advance for taking the time to check this out.
Objective:
To prevent duplication of incident numbers in the datasheet, and format the job number with a prefix of
Inc- at the beginning. I currently have the cell customization set to “Inc”General but that only inserts the prefix in the cells on the datasheet, but is not showing in the disabled textbox in the userform.
The Problem
I have a ‘Job Number’ that is generated each time the form is opened and when the ‘Save’ button is clicked the data from the form is transferred over
The job number is generated from the previous entry +1 (auto incrementing the old fashioned way).
The problem arises when the ‘Save’ button is pressed repeatedly, the same job number and data is duplicated on the datasheet.
Is there some way to ensure that the number generated is unique, and if the ‘Save’ button is repeatedly pressed that it will just over-ride the existing information?
The number format currently used is 20150003 (incremented by 1). But what I’d like to be displayed in the form is
Inc- 20150003
The following code is in the form_initialize procedure.
Me.txtSEC_INC_No.Enabled = True
Dim irow As Long
Dim ws As ws_Incident_Details
Set ws = ws_Incident_Details
'find last data row from database'
irow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Row
If ws.[a2].Value = "" Then
Me.txtSEC_INC_No.Text = 0 ' If no value in Col A, it will return a 0
Else
Me.txtSEC_INC_No.Text = ws.Cells(irow, 1).Value + 1
End If
I’d be really grateful if someone could help me out, or perhaps direct me to where I might find some coding that will achieve the result I am seeking.
I have just uploaded the latest version
My Sample form is linked to my Dropbox so you can see how it currently works (or doesn't work)
With much gratitude,
TheShyButterfly
Hope you have a terrific day, theShyButterfly

I am striving to improve my VBA but ... I am far from anywhere near in understanding the code that you have in your file. I feel really bad in saying that, but I am not a pretender, and will acknowledge when I am over my head.
I was thinking "simplified" :) ...
Don't worry, also Rom wasn't build in a day. :-)
I already answered the question about the duplication of the Job number in this thread:
https://social.msdn.microsoft.com/Forums/de-DE/52f3c62f-b26e-4573-b7c2-8e7203786d7f/excel-2010-vba-userforms-vlookup-via-textbox-display-result-in-another-textbox?forum=exceldev
So let us talk a little about the TAG property, thinking "simplified" and how to save the data:
Most people start with code like this when they start there first Userform:
Cells(MyRowNumber, 1) = txtBoxA
Cells(MyRowNumber, 2) = txtBoxB
etc. many many lines till
Cells(MyRowNumber, 56) = txtBoxWhatEver
And then, after Version 1.0, they realize that they also want to load data from a row into the form. And they copy all the lines and exchange
the parts before and after the
"=" like this:
txtBoxA = Cells(MyRowNumber, 1)
txtBoxB = Cells(MyRowNumber, 2)
etc. many many lines till
txtBoxWhatEver = Cells(MyRowNumber, 56)
And maybe you have another 56 lines to "clear" the Userform, and maybe more lines... over 150 lines just for this... that is really tremendous.
I will not be
too harsh,
if it works, then
it's okay.
But often many people struggle when they look into the code because, which column in the sheet is written by this line?
  Cells(MyRowNumber, 56) = txtBoxWhatEver
I've often seen that people change the code to this:
  Range("A" & MyRowNumber) = txtBoxA
  Range("B" & MyRowNumber) = txtBoxB
etc.  till
  Range("BD" & MyRowNumber) = txtBoxWhatEver
which is more clearly, but you must revise
150 lines!
And that is the point for the TAG property, which is in fact just a string. So when we write the column name ("A", "B", etc.) into the TAG property of a control, you can change the code to this:
  Range(txtBoxA.Tag & MyRowNumber) = txtBoxA
  Range(txtBoxB.Tag & MyRowNumber) = txtBoxB
etc.
And now the 1st trick, we can use a loop and visit all controls at once:
  Dim C As MSForms.Control
  For Each C In Me.Controls
    If C.Tag <> "" Then
      Range(C.Tag & MyRowNumber) = C
    End If
  Next
And when we want to load data from a row into the form, it's the same, just the other direction:
  Dim C As MSForms.Control
  For Each C In Me.Controls
    If C.Tag <> "" Then
      C = Range(C.Tag & MyRowNumber)
    End If
  Next
And to clear the form is also the same:
  Dim C As MSForms.Control
  For Each C In Me.Controls
    If C.Tag <> "" Then
      C = ""
    End If
  Next
So we can remove over 150 lines and do the same with just the 18 lines above.
Isn't that a simplification?
Think about that for a while.
Ready for the next trick? ;-)
As the TAG property is readable and writeable we can use Sub UserForm_Initialize and save a lot of manual work:
Private Sub UserForm_Initialize()
  Me.txtBoxA.Tag = "A"
  Me.txtBoxB.Tag = "B"
  'etc. till
  Me.txtBoxWhatEver.Tag = "BD"
End Sub
No time to waste,
here comes the next one. ;-)
In your file, you can have named ranges, but always have headings! And so we can get the column name e.g. from a named range:
  Me.txtBoxWhatEver.Tag = GetColumnName(Range("WhatEver"))
Function GetColumnName(ByVal R As Range) As String
Dim S As String
S = R.Address(1, 0)
GetColumnName = Left(S, InStr(S, "$") - 1)
End Function
Or you can use Range.Find and search for the header int the sheet and get the column name directly.
The benefit is that your form works even when the user change the layout of the sheet!
Simple
as it gets
(almost).
Andreas.

Similar Messages

  • How to stop Time Machine from backing up Temp Files?

    Hi folks!
    I am trying to find out how to stop Time Machine from constantly backing up 'useless' temporary files accumulated from web browsing? Although I don't change or add large files to my HD, Time Machine backs up hundreds of MBs every hour. Those file amounts make sense when I consider that I browse a lot to YouTube, SoundCloud, etc to watch videos or listen to music - I just don't want them to be backed up.
    Under Time Machine Options I already excluded the following items from backups:
    ~/Library/Caches
    /Library/Caches
    ~/Downloads
    But even though I did this weeks ago, Time Machine stills finds large files to be backed up every hour and my only guess is that I am still missing a location where either Safari or Firefox - the two web browsers I use - store temporary files.
    Can you help?
    Thanks a lot in advance!

    oas2103 wrote:
    they come from my anti-virus software
    Are you sure you need that? There are no viruses that run on OSX. None. Zip. Zero.
    If you're running Windoze on your Mac, that's the same as running it on a PC, so it needs all the same anti-everything stuff you'd use on a PC.
    There is some "malware," such as Trojans, for Macs, though. But (unlike viruses that can get onto your system without your knowledge), you must approve their installation (via your Admin password) and/or operation (via the "This application was downloaded from the internet ..." prompt).
    Appropriately enough, some of these Trojans are included in pirated versions of Apple software, such as iWork!
    For the gory details, see Thomas Reed's [Mac Virus Guide|http://www.reedcorner.net/thomas/guides/macvirus].
    Thanks a lot again!
    You're quite welcome, and thanks for posting back.

  • How to stop the Dialog from being dragged

    I was hoping that someone could tell me when calling a Dialog from Jframe, a how to stop the Dialog from being dragged
    while a dialog is showing.
    When it is visible I can still click and drag the Dialog
    I want to set it so you can not drag it until the dialog has be closed.

    If you don't have access to the parent frame, a "hack" that usually works:
    Frame frame = Frame.getFrames()[0];
    if (null != frame && frame instanceof JFrame){
    JFrame jf = (JFrame)frame;
    JDialog jd = new JDialog(jf, "title");
    ... code here ...
    As each JFrame (or Frame) is opened, its stored in the array of Frames that you can get. Same thing with Dialog.getDialogs(). Almost always, at least so far for me I've never had this problem, the [0] index is the main window opened, or the parent/top frame. I'd put the check in there to be safe and make sure its a JFrame and usually you'll only have the one JFrame.

  • How to stop the Browser from caching my web pages

    Hi There
    Can anyone tell how to stop the browser from caching my jsp pages
    I am using
    <%response.setHeader("Cache-Control","no-cache");
    response.setHeader("Pragma","no-cache");
    response.setDateHeader ("Expires", 0);
    %>
    but this does not work anduser is able to go back to the previous page
    which i dont want and i want the browser to display the message the page has expired
    Help appreciated
    Thanks
    Mumtaz

    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("max-age", 0);
    response.setDateHeader("Expires", 0);
    I have cut paste the code from my jsp which is working. There is one additional line. I do not know if that is the cause but there is no harm in trying that out.
    Srinivasan Ranganathan

  • How to stop  the users from changing the Decimal in SAP

    How  to stop  the users from changing User Profile

    Hai,
    It is not possible to restrict SU3 to display, because it has only S_TCODE has the authorization object.
    If you really want to restrict users from changing their profile you have to remove the SU3 access and give SU1 or SU2 which gives access only to Personnel details and Parameters.
    Hope this helps.
    Regards,
    Yoganand.V

  • How to stop the Calendar from editing your input?

    How to stop the Calendar from editing your input?

    I am constantly finding that Calendar Version 8.0 thinks it knows what I want to type but it incorrectly takes numbers and days and times out of my input and changes my appointments. I hate this new supposedly "smart" feature. Even when I edit my event correctly a second or third time it rewrites my input incorrectly again. I have to use my iPhone to override the annoying editing on my mac. I am ready to find a new calendar program unless Apple allows users to input what they want without their information being incorrectly edited.

  • How to stop the dock from showing all hidden images when hiding then showing applications

    How to stop the dock from showing all hidden images when hiding then showing applications

    It's not really something to snap a screen shot of. I believe it would need to be a video clip. For instance, when I am working on multiple images in Photoshop, I often need many images open at the same time, but I only have one image viewable and active. The hero image is active on screen while I hide the other images in the dock by hitting the yellow - button on the images. Then I "Hide" Photoshop to do something in the Finder. When I go back to Photoshop, every image that is hidden in the dock will pop open again in the active window, not just the image I was currently working on. I want these images to stay in the dock until I need them, no matter how many times I Hide the Show Photoshop.

  • How to stop PR creation from PM work order for a particular vendor?

    Hello Experts,
    We have following scenario for breakdown maintenance activities.
    When a machine breaks down, a breakdown order is created in SAP. The external manintenance services are planned in the order. When the order is released, a single PR (with multiple item numbers corresponding to number of services) is created by SAP. The PR has a release strategy. When PR is released, PO is created followed by the SES.
    The practice followed AS -IS for breakdown outside business hours:- If the breakdown happens outside business working hours, the person releasing the PRs is not available & hence PR can not be released resulting in no PO & no subsequent SES.
    In this case, the maintenance is completed by the external agency & the work order is TECO. The next day, TECO is reveresed, new PR is created, released, converted into PO, & then SES.
    TO-BE process:- Client expects that, for certain vendors identified, PR should not get created at all from the breakdown order. (The list of  vendors will be maintained seperately). For these vendors, a framework order will be created at the start of fiscal year & SES will be created as & when required agaainst the framework order.
    My concern is :- How to stop PR creation from PM work order for a particular vendor? OR delete the created PR?
    Highly appreciate your quick response.
    Thank you.
    Amit
    Note:- My ABAP consultant has checked following BADI's, but could not find it useful.
    IWO1_ORDER_BADI
    ORDER_COSTING_CK
    DATA_EXTENSION_CK
    VALUATION_CK
    ME_REQ_OI_EXT
    IWO1_PREQ_BADI
    ME_CHECK_SOURCES
    ME_REQ_POSTED
    IWO1_ORDER_BADI

    Dear Amit
    I am giving a different dimension to your question check if it fulfills your requirement -
    As we know Vendor selection comes at a later stage only after your PR creation, approval & RFQ etc. & PR generation from maintaine. order is controllled through control key PM03.  While triggering PR from order you have to specify Material group, Purchase group and vendor. Try to control it through authorisation as all these are authorisation objects. The persons executing breakdown order shall no t be given this authoriations
    shakti

  • Can anyone please tell me how to stop email previews from appearing on my screen. I have turned off the preview option in settings on both my ipad and iphone, but they still keep appearing.  Thanks

    Can anyone please tell me how to stop email previews from appearing on my screen. I have turned off the preview option in settings on both my ipad and iphone, but they still keep appearing.  Thanks

    works ok on mine - (5, 7.1.1).  Try again.  with previews set to none, mine shows the sender and the subject line only.  (you cannot get rid of the subject)    with the other settings, you get the sender, subject line and however many lines you selected.

  • HT2188 How to stop the battery from draining

    How to stop the battery from draining

    I tried everything to stop my battery from draining on my iPad 2 since I have downloaded IOS 7.... Without success. Very disappointed with IOS 7 so far. Apple please release a patch to stop the battery from draining. It is urgent. My iPad is now useless, it is always drained to 0% everytime I open my iPad!?!?!

  • How to stop volume icon from being displayed randomly

    How to stop volume icon from being displayed randomly

    Thanks for the quick response.
    I have a number of slides which act as a training module. The quiz follows this. All the answers to the quiz are in the module slides. I'd like the user to be able to see which questions they're failing on, but not be shown the answers, as, if they are failing the quiz, the whole module needs to be reviewed and the test retaken.
    If this isn't possible, I'll probably just remove the review option, thereby meaning my retake button can stay, and the user can just go back and re-read the module slides to learn the answers.
    Thanks

  • Any one know how to stop iTunes movies from playing menu music when movie is paused?

    Anyone know how to stop iTunes movies from playing menu music when the movie has been paused?

    JourneyMan24 wrote:
    Anyone know how to stop iTunes movies from playing menu music when the movie has been paused?
    Can't. This is done for the individual movie, not by iTunes.
    Some have it, most don't.

  • How to stop iphone 4s from showing last picture taken upon taking a new picture even if last picture was deleted

    how to stop iphone 4s from showing last picture taken upon taking a new picture even if last picture was deleted?

    how to stop iphone 4s from showing last picture taken upon taking a new picture even if last picture was deleted?

  • How to stop iphone 5S from downloading all my old emails

    How to stop iphone 5S from downloading all my old emails? I keep deleting the "latest"old emails and older ones just keeps popping up!

    Well Thank you for everyone's help.  I contacted the service provider, who uses google to support their email.  If anyone else has this problem....with google supported email here is the solution.
    Go to your email account
    click settings
    click "settings" (again) under the settings button
    click "forwarding and POP/IMAP"
    under POP download click "enable POP for mail that arrives from now on"
    save changes
    Then go to your iphone
    erase your email account
    re-install your email account
    now only new emails should show up on your phone, all your past emails will be saved at the server.

  • How to stop email notification from apple support site.

    how to stop email notification from apple support site. please inform me

    If you know the thread or community that is sending you the emails you can go directly to those and skip the rest of this however if you don't remember or are still getting email after going to the community read on.
    Stopping email from ths site can be difficult, it is a multi step procedure.  See the link Stopping emails from ASC which Neil posted for all the details.
    regards

Maybe you are looking for

  • Follow Up action for UD

    Hi, My client uses inspection type 89 for creating inspection lot. They have HU/SU management active in warehouse. When inspection lot is created for production batch, material stock and HU's are shown in quality inspection. When UD 1000 (Accept) is

  • Safari bounce but won't open

    Hello! I'm having a few problems with Safari 8.0.2 on Yosemite (10.10.1). Whenever I click the icon it bounce and the black dot appears, but it won't open and the dot disappears. Actually, the first time I open it after turning on the mac, it asks me

  • Returning Error Status back from Form to external program

    Question, I am trying to run a form essentially from the command line. I need to programatically catch any error codes that might have occured in the form by an external program. I can find nothing about returning errors back from the form or how to

  • WD abap calling iView

    hi folks i guess i have quite a simple question but.... i always called and iView over the control IFrame in WD Abap, but i got some problems so i heard i should use it with die if_wd_portal_integration methods, but i have no idea how. can anyone pro

  • Sorry, but still another question about ampersands in data.

    I'm sorry to raise a question that has been discussed before, but I have searched the forum and have not found anything I understand. The problem: I have a user requirement to link two pages using a column value on the first page that may contain emb