In Ada, specifying a Record’s Integer field as unbounded, probably box <> notation, in order to set the range when instantiating - record

Using GNAT CE with Ada on Macbook Intel silicon and want to specify (.ads) a record type with a type integer field, as unbounded,in order to set the record’s internet field’s range later, probably with box <> notation. But can’t get it right:
type a_gear is
record
num: Integer positive range <>; -- fails
position: Integer range <>) of integer; -- fails
end record;

Related

Variable in Dlookup in Access 2016 returns runtime error 13 - type mismatch

The logic isn't complex. The application uses a temporary table into which data for a report is appended. Once the report has been run, the data is cleared. Initially, the client told me that he would enter all the data (by taking attendance of people attending an event) would be done at once. Now, that has changed. I lookup the max ID in the temp table - if the number of rows is greater than 0. I then want to use dlookup to let the client know where she left off lest they create more problems.
Here's the logic:
Dim VMax As Variant
Dim VNFind As String
VMax = DMax("foodpantryid", "signin_sheet_data") 'This works correctly
MsgBox VMax 'This works correctly
VNFind = DLookup("last_name", "Dat_household_member", "[household_id]" = VMax) 'I get the type mismatch/error 13 here.
MsgBox VNFind 'so this never works
Additional information:
Household_id is a long integer. When I change it to integer, I get an error 94 (invalid use of null).
I have tried setting VMax to variant, integer, and long. Still no success. I would think variant would have worked.
The DLookup works if I don't include "[household_id]" = VMax.
I've tried including the =VMax in "" and that fails as well. That produces a 2471 error.
You've put your = outside the string delimiters.
This means: say your ID is 5. Then the parameter is "[household_id]" = 5. That's a comparison between the string "[household_id]" and the number 5, which causes a type mismatch, because you can't compare strings to numbers unless the string can be cast to a number.
Instead, you should include the = in your string, and the DLookUp should be:
DLookup("last_name", "Dat_household_member", "[household_id] = " & VMax)
Or, better yet, use parameters. This avoids most typecasting and string concatenation errors.
TempVars!VMax = VMax
DLookup("last_name", "Dat_household_member", "[household_id] = TempVars!VMax")
TempVars.Remove "VMax"

Detect text language in VBA

I have a textbox in PowerPoint which I store into an array with Split.
Is there any way to detect what language the text is in VBA?
There will actually only be English or Chinese text, so I guess an alternative solution would be to detect if the text is not English, or is/isn't Unicode?
It should be possible by checking that one of the characters is Chinese:
Function IsChiness(text As String) As Boolean
Dim c&, i&
For i = 1 To Len(text)
c = AscW(Mid$(text, i, 1))
If c >= &H4E00& And c <= &H9FFF& Then
IsChiness = True
Exit Function
End If
Next
End Function
The shape's .TextFrame.TextRange.LanguageID will tell you what language the text is set to. US English is 1033, for example. There's a list of language IDs here (use the Decimal LCID, right-hand column in this case):
https://msdn.microsoft.com/en-us/goglobal/bb964664.aspx?f=255&MSPPError=-2147217396
It's worth looking at the hex values as well. The rightmost two digits give you the main language code (Chinese is 04, for example) and the leftmost two digits identify the specific locale (PRC, Singapore, Taiwan, etc).
If you're likely to have mixed language text in a single text box, look at the LanguageID property of each .Run of text. For example, with a shape selected, try this:
Dim oRng As TextRange
Dim x As Long
With ActiveWindow.Selection.ShapeRange(1).TextFrame.TextRange
For x = 1 To .Runs.Count
Debug.Print .Runs(x).LanguageID
Next
End With

How to pass a date as an argument in an Excel VBA function

I'm trying to make a function that will take a date in a cell as an argument, then use that date to lookup a value. The date to be passed will be in the variable EffDate. Then the function should go to the worksheet RateChgs, check the NewPymtEffDateRange for the EffDate, and, upon finding it, go to the EscrowPymtAmtRange (one column wide) and return the value on the same row there.
I've only gotten to the point of testing it in the immediate window by typing GetEscrowPymt(8/1/2000) (or some other date). From the value of the Position variable, I can tell that the function isn't finding the date even though it's there. Is this a problem with how I'm passing the date?
Function GetEscrowPymt(EffDate As Date)
Dim PymtEffDateRange As Range
Dim EscrowPymtAmtRange As Range
Dim Position As Integer
Set PymtEffDateRange = Worksheets("RateChgs").Range("NewPymtEffDate")
Set EscrowPymtAmtRange = Worksheets("RateChgs").Range("EscrowPymt")
Position = Application.WorksheetFunction.Match(EffDate, PymtEffDateRange, 1)
MsgBox (Position)
End Function
The last argument in the Match function allows for returning an approximate match. If you require an exact match, then you should use the last argument of 0 to require an exact match. Otherwise, using the arguments 1 or -1 will return approximate match and assume also that the data is sorted ascending.
Position = Application.WorksheetFunction.Match(EffDate, PymtEffDateRange, 0)
The Match function will error if the effDate value is not found in the lookup array, so you may need error handling logic to account for that possibility. I would probably use the Application.Match function which can accept an error type, where the Match function in the Worksheet class will only accept long/integer values and will raise an error if the value isn't found:
Dim Position as Variant
Position = Application.Match(EffDate, PymtEffDateRange, 0)
If IsError(Position) Then
MsgBox EffDate & " not found!", vbInformation
Exit Function
' -- OR --
' assign some other return value for the function, etc.
End If
Some functions also have difficulty working with date values, so let me know if that doesn't solve the issue.
VBA also doesn't play well with various system locales, if you're expecting "8/1/2000" to be anything other than August 1, 2000, you may have more problems since VBA will interpret that by the US date format, not the system locale (e.g., in the UK that date would be 8 January, 2000). In that case, it may be best to treat the date as text and do a match based on text rather than date.

I am getting the error "Overflow." in Visual Basic 2010

Dim clicks As Integer
clicks = 0
If clicks >= 10000000000000000000 Then
a19.ForeColor = Color.FromArgb(0, 153, 0)
End If
10000000000000000000 and above gives the error.
Is there any way to have unlimited large length numbers?
You can use Long instead of Integer to allow you to go up to about 9 * 10E18 (9 followed by 18 zeros). Use the L suffix on literals if they are Long
Dim bigNumber as Long = 5000000000000000000L
If you want integers that can be any size at all, look at the BigInteger Structure. For example (the following requires a reference to the System.Numerics DLL as well as the statement Imports System.Numerics
Dim clicks As BigInteger = 0
'Code that updates click goes here
Dim limit As BigInteger = BigInteger.Parse("10000000000000000000")
If clicks >= limit Then
a19.ForeColor = Color.FromArgb(0, 153, 0)
End If
You can also use Double to hold number as large as 1E308, but it can't hold 308 decimal digits, so the numbers will only be approximations.
In Computer Science a number is most often represented as an integer. An integer is most often defined as a 32-bit signed integer, which means that the value can only hold up to a value from -(2^31) to (2^31)-1. To use bigger numbers, you might want to check other integer types. The most commonly used bigger integer, is the 64-bit integer, which can hold a value from -(2^63) to (2^63)-1.

#VALUE error with Excel VBA Function

In my Excel spreadsheet I have two columns.
A contains strings with the values 'Yes', 'No' or 'Maybe'.
B contains strings with a year in.
I need a function to determine the number of occurrences of a year in column B, where the equivalent value in column A is 'Yes'.
I currently have the following code:
Function CountIfYearAndValue(Rng As Range, YNM As String, Year As String) As Integer
Dim count As Integer
count = 0
For Each c In Rng.Cells
If (StrComp(Abs(c.Value), Year, vbTextCompare) = 0) And (StrComp(Cells(c.Row, A), YMN, vbTextCompare) = 0) Then count = count + 1
Next
CountIfYearAndValue = count
End Function
The idea of this code is that we iterate through every cell in the range given (a range on column B) and check if the year is equal to the Year parameter. And if the equivalent cell on column A is equal to the YNM parameter we increment the count variable.
For some reason this code does not work when I use the following parameter:
=CountIfYearAndValue('Years'!B1:B7,"Yes","Year 7")
It just does the #VALUE error and refuses to display any outcome.
Any help would be much appreciated.
Edit: All of the values in both cells are on of an unformatted datatype ('General') and no cells are blank.
It sounds like you are reinventing the wheel... There already is a built in function (advantage: being much faster than a UDF) that does exactly what you are after. It is called COUNTIFS()
All YESes for Year 7 in rows 1 to 10.
=COUNTIFS(B1:B10, "Year 7",A1:A10, "Yes")
I just had a quick look at your code and I think there are possibly a few reasons why your original code is not working as expected.
YNM is a valid column name therefore it should not be used as a variable name. You should avoid naming your variables like that - give it a more meaningful name
YNM != YMN as you had it in your code (see function definition and then the misspelled version in the StrComp() function)
Year is a valid VBA built in function, therefore once again you should avoid using it as a variable name as you're exposing yourself to a naming collision.
Add Option Explicit at the top of your module. This requires you to Dimension all you variables. It's always recommended for many many reasons.
rng variable is of Range type therefore you do not need to explicitly add the .Cells property to it. Even though it may help in some cases - at a bit more advanced level you may face some runtime type compatibility issues. ( runtime may convert your rng Range variable to a 2D array etc )
Added an explicit conversion in the second StrComp() function around the c.Offset(0, -1) as you don't want the runtime to (rare but still possible) convert your Yes to a Boolean data type. Explicit conversion to a String just gives you that extra protection ;p (lol)
therefore, something like this returns the correct value
Function CountIfYearAndValue(rng As Range, choice As String, myYear As String) As Long
Dim count As Long
count = 0
Dim c As Range
For Each c In rng
If (StrComp(c, myYear, vbTextCompare) = 0) And (StrComp(CStr(c.Offset(0, -1)), choice, vbTextCompare) = 0) Then
count = count + 1
End If
Next c
CountIfYearAndValue = count
End Function
Right, I hope this helps you understand bits and pieces :) any questions please leave a comment