For a registration tool we always created the case ID by the following line of code.
Me.RMA_Nr = Format$(Now(), "yymm") & Format$(Val(Right$(DLast("[RMA-Nr]", "TBL_RMA_ISSUE"), 3)) + 1, "000")
unfortunatly we get now always the same issue number where should I look where this is going wrong. this line worked for over a year.
Use DMax:
Me.RMA_Nr = Format$(Date(), "yymm") & Format$(Val(Right$(DMax("[RMA-Nr]", "TBL_RMA_ISSUE"), 3)) + 1, "000")
Related
I am trying to make a program that searches for specific content in cells for excel:
rows represent hours of the day from 00:00 to 23:00
columns represent a day of the month
The following code matches the input of the user to the content of the cells either by using the name. It can also add or skip a time interval for the event
However the following code always runs the instructions under Else even if the user inputs 2 numeric values for the 2 param fields. Some help would be appreciated:
pNume = paramNume
aux = ""
aux1 = paramHBegin - 1
aux2 = paramHEnd - 1
If IsNumeric(paramHBegin) And IsNumeric(paramHEnd) Then
For i = 1 To 31
For j = aux1 To aux2
If Cells(i + 1, j + 1) Like pNume & "*" Or Cells(i + 1, j + 1) Like "*" & pNume & "*" _
Or Cells(i + 1, j + 1) Like "*" & pNume Then
aux = aux + Cells(i + 1, j + 1) + " la ora " + CStr(i) + vbCrLf
End If
Next j
Next i
Else
For i = 1 To 31
For j = 1 To 24
If Cells(i + 1, j + 1) Like pNume & "*" Or Cells(i + 1, j + 1) Like "*" & pNume & "*" _
Or Cells(i + 1, j + 1) Like "*" & pNume Then
aux = aux + Cells(i + 1, j + 1) + " la ora " + CStr(i) + vbCrLf
End If
Next j
Next i
End If
displayInfo.Text = aux
This statement
If IsNumeric(paramHBegin) And IsNumeric(paramHEnd) Then
will only be true if both of the values you pass are numeric. But you wrote that "However the following code always runs the instructions under Else even if the user inputs 2 numeric values for the 2 param fields."
Assuming paramHBegin and paramHEnd are defined as strings, the only way I can see that happening is for one or both of the values having a character that isn't numeric. In Excel 2013 IsNumeric ignores carriage returns, tabs and spaces so they aren't the cause of the problem. If they are defined as objects, then you should specify the correct property of those objects.
Sorry I can't try it myself, but try something like this:
If IsNumeric(paramHBegin.Text) And IsNumeric(paramHEnd.Text) Then
or this:
If IsNumeric(val(paramHBegin.Text)) And IsNumeric(val(paramHEnd.Text)) Then
I think it's better to handle user inputs right at the input session itself rather then passing everything forward and then trying to handle exceptions
for instance, to force (to some extent) numeric inputs you could place in the UserForm code pane what follows:
Option Explicit
Private Sub paramHBegin_AfterUpdate()
TextBoxValidate Me.paramHBegin
End Sub
Private Sub paramHEnd_AfterUpdate()
TextBoxValidate Me.paramHEnd
End Sub
Private Sub paramNume_AfterUpdate()
TextBoxValidate Me.paramNume
End Sub
Sub TextBoxValidate(ctrl As MSForms.TextBox)
Dim number As Double
Me.CommandButton1.Enabled = False
With ctrl
If Not ValidateText(.text, number) Then
MsgBox "invalid input", vbCritical
.SetFocus
Else
.text = number
Me.CommandButton1.Enabled = True
End If
End With
End Sub
Function ValidateText(text As String, number As Double) As Boolean
On Error Resume Next
number = CDbl(WorksheetFunction.Substitute(text, " ", ""))
ValidateText = IsNumeric(number)
End Function
in essence:
add an AfterUpdate event handler for every relevant TextBox
that event handler would simply pass the validation dues to a specific sub (TextBoxValidate()) where you can put code to handle general TextBox validation environment
for instance I
disabled CommandButton1 button (the "OK" one in my test)
call a function (ValidateText()) for validating the Text property of a TextBox control which would return:
True and the validated number, if the Text property were actually a possible number
False otherwise
if validation result is True:
update the TextBox with the validated number
enable the "OK" Button
if validation result is False:
prompt a message and return the focus to the "invalid" TextBox
of course you can tune all those Subs and Functions to your actual needs like:
checking for specific text formats (with Like operator or - better - with RegEx object)
checking for specific types ( using Clng() or CInt() or CDate() instead of CDbl() in a sort of TryParse() fashion
changing validation rules according to each control type (TextBox rather than ListBox and so on) and property (Text Property rather than Value ...)
Finally
use "&" operator instead of "+" one to concatenate strings
If Cells(i + 1, j + 1) Like pNume & "*" Or Cells(i + 1, j + 1) Like "*" & pNume & "*" _
Or Cells(i + 1, j + 1) Like "*" & pNume Then
can be reduced to:
If Cells(i + 1, j + 1) Like "*" & CStr(pNume) & "*" Then
hope all this can help
I have a query that I execute through VBA in Access 2010. The result of the query should be AFR, but it returns AFR with an extra line below it. I have added the "'" character to make the extra line visible.
TempHold = rs.GetString
Debug.Print "'" & TempHold & "'"
Returns this:
'AFR
'
But should return this:
'AFR'
I have tried using the below code, but none of the If statements evaluate as True. The code should check for a " ", a vbNewLine, or vbCrLf character but none evaluate as true. Does anyone know of any additional characters that would result in a new line?
If Right(TempHold, 1) = " " Then
TempHold = Left(TempHold, Len(TempHold) - 1)
ElseIf Right(TempHold, 2) = vbNewLine Or Right(TempHold, 2) = vbCrLf Then
TempHold = Left(TempHold, Len(TempHold) - 2)
End If
Use:
Asc(Right(TempHold, 1))
to get the Ascii character code.
Once you've found the character code (which, as you wrote in your comment, was 13), you can use your code to remove it:
If Right(TempHold, 1) = Chr(13) Then
TempHold = Left(TempHold, Len(TempHold) - 1)
End If
In this case, you can also use vbCr, which is the same as Chr(13).
The best way to get rid of the carriage return in my opinion is to stop it being created in the first place. This method is a lot tidier than having to remove the last character.
In the .GetString method there is a parameter for RowDelimiter which by default is set to be a carriage return. However you can change this to be whatever you want including a zero length string as follows:
rs.GetString(, , , "")
If you run your debug again with this code:
rs.GetString(, , , "")
Debug.Print "'" & TempHold & "'"
You will get this result:
'AFR'
Remember if you want something different to be placed between rows then just change the zero length string to whatever you need.
I'm really confused as to why I have 2 For loops that aren't initiating. I have others in my macro that work fine, however. I even tried pulling the code sets out into different subs to see if the other code affected it, however it still didn't work! (This code worked fine yesterday... and the declarations are all correct (used elsewhere and reset fine).
One example:
strVal = regionArr(counti, 3)
For textSearch = 6 To lastrowVF
If UCase(lesTab.Range("C" & textSearch).Text) Like UCase(strVal) & "*" Then
inTot.Range("K" & 2 + county).Value = lesTab.Range("C" & textSearch).Offset(0, 1).Value
inTot.Range("L" & 2 + county).Value = lesTab.Range("C" & textSearch).Offset(0, 2).Value
county = county + 1
End If
Next textSearch
I checked here and all over the web and found a few solutions to this that I tried, including casting Now as a CDate, but nothing works.
Using the VBA Editor w/ Excel 2010 on Win7.
Even the example code from the help section is throwing this error and the date used in the Month() function is explicitely initialized in numeric form.
In both cases I tried using CDate() on the month() argument, it does not work. In my original code I also tried using Date instead of Now, no effect either.
Here is my original code, which casts the error on the If condition:
Function SetNextTaskNb()
Dim seqNb As String
seqNb = ThisWorkbook.Worksheets("Persistent").Range("A" & 1).Value
Dim Nbs() As String
Nbs = Split(seqNb, ".", 2)
Dim month, currentNb, nextNb As Integer
month = CInt(Nbs(0))
currentNb = CInt(Nbs(1))
If month(Now) = month Then
nextNb = currentNb + 1
Else
nextNb = 1
End If
ThisWorkbook.Worksheets("Persistent").Range("A" & 1).Value = currentMonth + "." + nextNb
ThisWorkbook.Worksheets("Sheet1").Range("A" & 1).Value = currentMonth + "." + nextNb
End Function
Here is the example code from the VBA Editor Help section, which, copy-pasted with no modifications throws the same error on the first Debug.Print. It won't even display it.
Dim MyDate, MyMonth
MyDate = #2/12/1969# ' Assign a date.
Debug.Print "month is " & month(MyDate)
Debug.Print "rgMonth.Cells(i, j).Value is " & rgMonth.Cells(i, j).Value
MyMonth = month(CDate(MyDate)) ' MyMonth contains 2.
I know I'm supposed to give the Month() function a date in numeric form, and that it returns an Integer. I tried everything I could think of.
Here is proof this is the code from the Help:
you are using Month as both the name of a function and the name of a variable................give the variable a different name.
How do I prompt the user to input elements in a two dimensional array? And how do I then save the output?
I've figured out how write and print a program where the program provides the elements (see short version below), but I can't work out how to get the user to input the elements instead.
Many thanks.
Solfa(0, 0) = 11
Solfa(0, 1) = 12
Solfa(1, 0) = 21
Solfa(1, 1) = 22
TextBox1.Text = Solfa(0, 0) & " " & Solfa(0, 1) & vbCrLf & Solfa(1, 0) & " " & Solfa(1, 1)
Consider a grid (if your presentation framework has one).
Otherwise, you can use an multi-line textbox that is pre-populated with a comma-separated list of values, like this:
11, 12
21, 22
Then, you can parse the user-edited values again (the easiest way being)
For Each line in input.Split (vbCrLf)
For Each field in line.Split (", ".ToCharArray())
// Plug back into array.
// Don't forget bounds-checking.
Next
// Don't forget bounds-checking.
Next