VBA Question - find closest next number

Hi,

I have a form with a combobox control that list some sizes.  They are:

12
18
20
22

I have another text box for keying a custom size, such as 12'2".  What I 
would like to do is have the combobox default to the next size (in this case 
18).  Here's the code I have used so far (it does a little more than what I 
am asking for but to give an idea):

Dim MCUST As String

MCUST = Me.custwidth

If Str(Me.widthck) <> Me.custwidth Or Len(Me.custwidth) <> Len(Me.widthck) 
Then

        Me.widthck.Value = Int(Val(MCUST)) + 1
    
    If InStr(Int(Me.widthck) / 2, ".") > 0 Then
        Me.widthck = Me.widthck + 1
    End If
    
    If Me.widthck < 12 Then
        Me.widthck = 12
    End If

End If

If Len(Me.custwidth) = Len(Me.widthck) And Val(Me.custwidth) Mod 2 = 0 Then
    Me.widthck = Me.custwidth
End If

Any help is appreciated.

Thanks,

Bonnie

0
Utf
12/19/2007 4:39:03 PM
access 16762 articles. 2 followers. Follow

3 Replies
1534 Views

Similar Articles

[PageSpeed] 48

Bonnie,

You might consider a function using a select case statement like as follows>

Public Function NextSize(dblActualSize As Double) As Integer
Select Case dblActualSize
    Case Is <= 12
        NextSize = 12
    Case 12 To 18
        NextSize = 18
    Case 18 To 20
        NextSize = 20
    Case 20 To 22
        NextSize = 22
    Case Is > 22
        NextSize = 22
End Select
Debug.Print "Next size is " & NextSize
End Function

Ken Warthen
kenwarthen@gmail.com



"Bonnie" wrote:

> Hi,
> 
> I have a form with a combobox control that list some sizes.  They are:
> 
> 12
> 18
> 20
> 22
> 
> I have another text box for keying a custom size, such as 12'2".  What I 
> would like to do is have the combobox default to the next size (in this case 
> 18).  Here's the code I have used so far (it does a little more than what I 
> am asking for but to give an idea):
> 
> Dim MCUST As String
> 
> MCUST = Me.custwidth
> 
> If Str(Me.widthck) <> Me.custwidth Or Len(Me.custwidth) <> Len(Me.widthck) 
> Then
> 
>         Me.widthck.Value = Int(Val(MCUST)) + 1
>     
>     If InStr(Int(Me.widthck) / 2, ".") > 0 Then
>         Me.widthck = Me.widthck + 1
>     End If
>     
>     If Me.widthck < 12 Then
>         Me.widthck = 12
>     End If
> 
> End If
> 
> If Len(Me.custwidth) = Len(Me.widthck) And Val(Me.custwidth) Mod 2 = 0 Then
>     Me.widthck = Me.custwidth
> End If
> 
> Any help is appreciated.
> 
> Thanks,
> 
> Bonnie
> 
0
Utf
12/19/2007 9:17:02 PM
Maybe I'll have to do that but I was hoping that I missed a function that 
might locate the next highest number.  There are just so many.

Thanks Ken.

"Ken Warthen" wrote:

> 
> Bonnie,
> 
> You might consider a function using a select case statement like as follows>
> 
> Public Function NextSize(dblActualSize As Double) As Integer
> Select Case dblActualSize
>     Case Is <= 12
>         NextSize = 12
>     Case 12 To 18
>         NextSize = 18
>     Case 18 To 20
>         NextSize = 20
>     Case 20 To 22
>         NextSize = 22
>     Case Is > 22
>         NextSize = 22
> End Select
> Debug.Print "Next size is " & NextSize
> End Function
> 
> Ken Warthen
> kenwarthen@gmail.com
> 
> 
> 
> "Bonnie" wrote:
> 
> > Hi,
> > 
> > I have a form with a combobox control that list some sizes.  They are:
> > 
> > 12
> > 18
> > 20
> > 22
> > 
> > I have another text box for keying a custom size, such as 12'2".  What I 
> > would like to do is have the combobox default to the next size (in this case 
> > 18).  Here's the code I have used so far (it does a little more than what I 
> > am asking for but to give an idea):
> > 
> > Dim MCUST As String
> > 
> > MCUST = Me.custwidth
> > 
> > If Str(Me.widthck) <> Me.custwidth Or Len(Me.custwidth) <> Len(Me.widthck) 
> > Then
> > 
> >         Me.widthck.Value = Int(Val(MCUST)) + 1
> >     
> >     If InStr(Int(Me.widthck) / 2, ".") > 0 Then
> >         Me.widthck = Me.widthck + 1
> >     End If
> >     
> >     If Me.widthck < 12 Then
> >         Me.widthck = 12
> >     End If
> > 
> > End If
> > 
> > If Len(Me.custwidth) = Len(Me.widthck) And Val(Me.custwidth) Mod 2 = 0 Then
> >     Me.widthck = Me.custwidth
> > End If
> > 
> > Any help is appreciated.
> > 
> > Thanks,
> > 
> > Bonnie
> > 
0
Utf
12/19/2007 9:41:00 PM
The approach to this problem is to simply put a little bit of code in the 
after update event of that control box (custom size), and have the code set 
the value in the combo box.

You're also correct and your assumption to not use a hard coded case 
statement as the other poster suggested.

I would try somthing like the following code in the after update event of 
that text box. The following code simply selects the topmost or closest next 
higher value from your given table.

   Dim strSql        As String
   Dim rst           As DAO.Recordset

   If IsNull(Me.CustSize) = False Then

      strSql = "select top 1 id from tblSizes where Size  <= " & Me.CustSize 
& _
               " order by Size DESC"
      Set rst = CurrentDb.OpenRecordset(strSql)
      If rst.RecordCount > 0 Then
         Me.Combobox = rst(0)
      End If
      rst.Close
      Set rst = Nothing
   End If

The above sample assumes that your combo box is actually a two collum combo 
box in which you're displaying a value, but actually storing the primary key 
or ID, this is a very typical setup in MS access .

If you only have one column for the combo boxes data source, then you can 
simply modify the above a bit it should work quite fine.


-- 
Albert D. Kallal    (Access MVP)
Edmonton, Alberta Canada
pleaseNOOSpamKallal@msn.com


0
Albert
12/20/2007 12:45:40 AM
Reply:

Similar Artilces:

VBA code to say Yes or No if any VBA code is present in ActiveWork
Using XL 2003 & 97 Would like hit Ctrl+Shift+V and have an answer either yes or no as to the presence of Any VBA code in the Active Workbook. I do know that about the VBA editor/Explorer or to press the Run Macro button and get a list from various sources . I would like to stay at the keyboard and process a series of macros including one to let me know if I even need to evoke the VBA explorer. TIA Dennis There is no bootstrap macro to tell you whether you have macros or not. If there were there would be no limit to virus writers. --- HTH, David McRitchie, Microsoft MVP - Excel ...

Simple question on Cursor
Hello, I have a simple dialog box application and I would like to change the cursor from an arrow to an hourglass when I press a button. In my OnClick event, I have this code void Button::OnClick() { hCursor=AfxGetApp()->LoadStandardCursor(IDC_WAIT); .... hCursor=AfxGetApp()->LoadStandardCursor(IDC_ARROW); } Can someone explain to me on how to get this working? Thank-you Hello Try using BeginWaitCursor() then later EndWaitCursor() Regards, Elias "dmoss22922" <dmoss22922@email.msn.com> wrote in message news:eoJxEphcDHA.2904@TK2MSFTNGP11.phx.gbl... > Hello,...

Print, Merged Cells, and VBA
Hello I'm looking for a code that will help me with a print job for a worksheet. There are a lot of merged cells and a lot of "IF"s to be used in the VBA for range selection. Thanks in advance, Sarr ...

Add sequences of positive then negative numbers
I have a long list of random positive and negative numbers in column A. Id like to be able to add sequences of positive then negative numbers as they occur. For example... in the list 5,-1,-5,-3,2,8,-1,-3,9,7,5,11,-5,6,5 ... i would add the positive numbers until the next number in the list was negative, then add the negative numbers till the next number was positive, etc. The answers for the above list would be 5, -9, 10 , -4, 21, -5, 11 Any ideas ? -- judoist ------------------------------------------------------------------------ judoist's Profile: http://www.excelforum.com/mem...

Use VBA to update Access table or Query from Excel
Can I use VBA to update Access table or Query from Excel? Thanks in advance Hi Leungkong, > Can I use VBA to update Access table or Query from Excel? Of course, using ADO or DAO. See: http://www.erlandsendata.no/english/index.php?d=envbadacexportado Regards, Jan Karel Pieterse Excel MVP http://www.jkp-ads.com Hi Jan, Thanks. I think ADO is what I want. But I am not only want to export from excel to access. I want to edit some data in access table. For example, Access has a table "ProductList" I want to use Excel to call the product by "Pro...

VBA and Scheduled task in Outlook
Hi, how can I: a) pull the query from somewhere and send it by e-mail automatically. E.g. I want to take a temperature table from http://www.wunderground.com/history/airport/EHAM/2009/7/10/DailyHistory.html?req_city=NA&req_state=NA&req_statename=NA where 2009/7/10 is the date of yesterday put it into an e-mail and send it automatically each day at 8 o'clock? b) if this is not possible, how can I send an e-mail with the attachment c:\Documents\query20090710.xls each day at 8 o'clock? my boss wants this automatized, I know how to write the VBA code for Excel, but don'...

auto number
I was wondering how to go about setting up a work sheet to so as soon as I click in the first row that I will be adding data it would automaticly mark that row Number 1 and when I enter new data in the second row it automatically puts in 2, and so on? Thanks -- Eddie Hi Eddie - Try this: (Excel 2003) You don;'t give much info about how your spreadsheet is set up so try this: Let's assume you have all of your field names in the first row and the A col will hold the auto number. 1. In a2 type: =cell("row",a2) 2. Select Data from the menu 3. Select Form from the dro...

VLOOKUP in VBA
On the worksheet I can insert in a cell =VLOOKUP(C5, Hobokee.xls!AcsLow, 2) and it works perfectly, looks up the value in column 2 of the range named AcsLow in the same workbook. But elswhere I want in a macro to lookup the same table and assign the result to a variable BalAmt. BalAmt = VLOOKUP(AccNum, Hobokee.xls!AcsLow, 2) does not work. It gives a function not defined error on Hobokee. If instead of Hobokee I put Workbooks("Hobokee"), it gives function not defined error for VLOOKUP which it changes to VLookup. I have been overVLOOKUP in the Help file and see nothing wrong. W...

Column width and pasting sections in excel? Formatting questions
I am trying to make a spreadsheet in which I need to have varied column widths in different sections, one under another. (they don't need to relate directly, and no major equations going on) for example: xxxxxxxxx|xxxxxxxxxx xxxxxxxxx|xxxxxxxxxx xxxxxxxxx|xxxxxxxxxx xxxxxxx|xxxxx|xxxxxx xxxxxxx|xxxxx|xxxxxx How do I go about splitting the sheet or whatever I need to do so I can manipulate columns differently based on the row I'm in? Thanks! Rich Column widths apply to the whole column. You may be able to use merged cells to give the appearance that you want, but I try to stay...

vba select field
Hi, having a button on a form, is it possible to select only some fields (all from the same table) of the current record instead of all fields? I've tried this code: RunCommand acCmdSelectRecord RunCommand acCmdCopy Forse dipende dei dati nel record. but it selects ALL fields in the form. Thank you. Remigio hi, On 10.04.2010 18:01, remigio wrote: > having a button on a form, is it possible to select only some fields > (all from the same table) of the current record instead of all fields? > I've tried this code: > > RunCommand acCmdSelectRecord &...

Revision conversion to a number suggestions ?
I have a problem with Revisioning conversions. I wonder if any one has any ideas on how to do this better. One of the elements in my database is drawing numbers and their Revision letter. ie drawing GA123 Revision A . the next drawing would be GA123 B and so on. My problem is when the drawing rolls all the way to GA123 AA or Even 3 Letters in the revision ie Rev ABZ. Currently i take the asc value of each letter and add them togeter to get a number representing the revision. example Rev AB would be 65+66 = 131 so when i compair drawings to see if a new one exists i can just...

Number Format #10
Hello I've got a large spreadsheet which was imported. Apart from the first 2 columns, the rows consist of number which I need to filter, but this isn't working. Then I noticed an apostrophe before each number in every cell, which doesn't show in the cell but up in the formula/text bar. I've set all the columns to numbers but the apostrophe won't go away. Please can someone tell me how I can without having to go into each cell as there are hundreds. Thank you J Select all the columns with these "numbers" in them, then right click the tab at the bottom of t...

Turn pc speaker on/off with vba code
Is this possible? Thanks I found something here on how to do it withVBScript. You can probably modify it to work in VBA http://www.pcreview.co.uk/forums/thread-1468591.php -- HTH, Barb Reinhardt "David" wrote: > Is this possible? > Thanks Thanks Barb I'll have a go (I need to improve my web searching skills) "Barb Reinhardt" wrote: > I found something here on how to do it withVBScript. You can probably modify > it to work in VBA > > http://www.pcreview.co.uk/forums/thread-1468591.php > -- > HTH, > ...

Add a set number to a random entered number
I have to start a vehicle service record and am trying to add the 6000 mile service interval to the mileage at service number. EG. A B C D 1 6000 12000 2 12125 18125 3 18200 24200 etc A is the mileage at service and C is the next service due.I am trying to do this without having a cell with a line of 6000 in it and the formula eg =SUM(A1:B1) in the C cell.Is this possable?Hope some one can help. Hi =A1+6000 HTH. Best wishes Harald "Cyclops" <Cyclops@discussions.microsoft.com> skre...

Excel VBA/Macro programming for beginners
Hi all, I know a bit VB 6.0 programming. Will it help me to write exce vba/macro(same thing?).can anybody help me by providing some fre websites where i can have informations/codes for VBA/macro. Please tell me how could i make a *command Button * in a cell in th excel sheet -- sdebu_200 ----------------------------------------------------------------------- sdebu_2000's Profile: http://www.officehelp.in/member.php?userid=430 View this thread: http://www.officehelp.in/showthread.php?t=118812 Posted from - http://www.officehelp.i Try David McRitchie's "getting started with mac...

Access 2003 Form Question
I have an access database that I am using for site surveys. In a nutshell, there is a header table with a date and location as the keys and a detail section with the survey results. The detail section shows a series of 30 or so questions(from a questions table) on how the site was rated. I am trying to design a form that populates a yes/no/not available type response in the detail section. The user would either click on a yes/no/NA type box or have a drop down and select the value(whichever is easiest) next to the box. After the user answers the 30 or so questions, an answers table...

THE BIGGEST QUESTION OF ALL
After months trying to make things work, and now I found errors that did not come from my own ignorance but from MSRMS such as: --Wrong item description misteriously appearing on my matrix. --Zebra printer not printing hang tags properly. --lots of other questions unanswered, as you all here are asking... The big question is: If we pay $90 per incidence, it will cost thousands, but are there solutions? Consequently, shouldn't MS be responsible for the flaw in its software and provide this kind of technical support to be worth the original month we pay with the assumption that the...

Number of categories
I need to make a macro to change the between tick-mark labels with a value depending of the total numbers of rows, but I do not now the VB code to put the number of categories in the graph. I'm not a expert in VB coding, any help is appreciated, Thanks, jos post specifics "Jos" wrote: > I need to make a macro to change the between tick-mark labels with a value > depending of the total numbers of rows, but I do not now the VB code to put > the number of categories in the graph. > I'm not a expert in VB coding, any help is appreciated, > Thanks, jos ...

Loading a Form from VBA
Hi all, I'd like a module to open and load a form in my Access 2003 database... How can I do this? Use the OpenForm method. Look in the Help file for details. -- Doug Steele, Microsoft Access MVP http://I.Am/DougSteele (no e-mails, please!) "Mike" <michael.leon.schwartz@gmail.com> wrote in message news:1184770076.109526.207900@o11g2000prd.googlegroups.com... > Hi all, I'd like a module to open and load a form in my Access 2003 > database... How can I do this? > On Jul 18, 10:58 am, "Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com&g...

Excel and VBA
I am very interested in learning more about using VBA in office, especially Excel. Can you post some resources (online, books, etc) that can be used to help learn about using Visual Basic in office. Thanks! Here's a good place to start: http://www.contextures.com/xlbooks.html Does that help? *********** Regards, Ron XL2002, WinXP "Bob Sinclair" wrote: > I am very interested in learning more about using VBA in office, especially > Excel. Can you post some resources (online, books, etc) that can be used to > help learn about using Visual Basic in office. >...

VBA Customization
Is there a way to get the name of the current company through VBA in Great Plains 8.0? Thanks for all your help. open GreatPlains. Add the current window, it will add the Toolbar then add fields choose the company name on the top left after the user id. >-----Original Message----- >Is there a way to get the name of the current company through VBA in Great >Plains 8.0? > >Thanks for all your help. > > > >. > Thanks. I tried, but I couldn't add company name to the field list. It opens up the login screen instead. Any thoughts? Thanks again. "ger...

Run VBA without show
I want excel to run the VBA code without showing me what it is doing. Which code should i use for that? Sub RunWithoutShow() Application.ScreenUpdating = False 'Paste the vba code here 'end of code 'Change screenupdating property to true Application.ScreenUpdating = True End Sub I hope this helps... Selva V Pasupathy For more on Excel, VBA, & other Resources Please visit: http://socko.wordpress.com You may also (inserting into quote from code example bellow from Socko) Use Application.Visible property to hide the application entirely Sub RunWithoutShow() Appli...

possible to enumerate page numbers?
Hi, If any CRM object has big data(let's suppose that Opportunity has 50000 data), in the View, you can see those data by page number..... However, by default, it only navigates by one page...In other words, if I want to see 15 page, I should click 15 times to look at the page... Any way page number can be customized via CRM system customization?... I couldn't find it... For example, if I want to have viw something like this... 1 2 3 4 5 6 7 ..........(Would be last number of page number) In this way, if I want to look at the 15 page, I only need to click 15 number... Thanks....

Form Controls vs VBA controls
I am new to VBA so just finding my feet, can anyone help me with th differences between an excel control (i.e. one from the form toolbox lets say a checkBox and a vba checkBox? From trying the two controls the I have noticed is that you canno capture the change event of the VBA checkbox in the code module, it ha to be done via the sheet (i.e. checkbox on sheet1, code goes in chang event on sheet1) the control is placed on, is this correct? What woul you do if you have three sheets with the same controls just differen data, do you really have to code the control 3 times? The excel control ho...

change a number (100) to word (i.e Hundred)
Dears, Is there a function or a way to change automatically a number typed in a cell (eg: 100) as in tex format? (i.e Hundred)? Kind regards, Abdulssalam CH Take a look at http://www.xldynamic.com/source/xld.xlFAQ0004.html Excel FAQs - Convert Numbers To Text -- HTH RP (remove nothere from the email address if mailing direct) "CHA Salam" <CHA Salam@discussions.microsoft.com> wrote in message news:DE427447-1B13-4C64-812A-CF7AF5213EE1@microsoft.com... > Dears, Is there a function or a way to change automatically a number typed > in a cell (eg: 100) as in tex for...