error occurred from "clear Button" there are two field with data type with date& time
Private Sub cmdClear_Click()
Me.SampleCode = ""
Me.ReceivedDate = ""
Me.SampleQuntity = ""
Me.SampleReceivedTime = ""
Me.SampleName = ""
Me.BatchNo = ""
Me.MRANo = ""
Me.Division = ""
Me.SubmittedBy = ""
Me.ReceivedBy = ""
Me.AssignedTo = ""
''focuse on SampleCode txtBox
Me.SampleCode.SetFocus
End Sub
Without any further information to go on it's either:
Your received date can't accept any empty string.
If it can't be Null then set it to something like 01-01-1900.
Any fields that are numeric should be set to Null or 0.
It could be that you've misspelt SampleQuntity. Should it be SampleQuantity?
Related
I am trying to have a way to let the user click multiple checkboxes and lists, to filter a subform. When I try to use an AND statement, I get an error: Run-time rror '13': Type mismatch. I put an arrow to the line in question. Is an And statement not valid syntax for filtering this way?
Private Sub SearchB_Click()
If Me![CoreCB] = True Then
Me.Query1SF.Form.Filter = " IsDate([Core RS]) = True"
Me.Query1SF.Form.FilterOn = True
ElseIf Me![SiteCB] = True Then
Me.Query1SF.Form.Filter = " IsDate([Site RS]) = True"
Me.Query1SF.Form.FilterOn = True
If Not IsNull(SiteCombo.Value) Then
--> Me.Query1SF.Form.Filter = "[Location] = '" & Me.[SiteCombo].Value & "'" And " IsDate([Site RS]) = True" '!! THIS DOESN'T WORK
Me.Query1SF.Form.FilterOn = True
End If
ElseIf Me![SecurityCB] = True Then
Me.Query1SF.Form.Filter = " IsDate([Security]) = True"
Me.Query1SF.Form.FilterOn = True
End If
End Sub
String expression for form filter seems incorrect. Use below line
Me.Query1SF.Form.Filter = "[Location]='" & Me.[SiteCombo].Value & "' And IsDate([Site RS])=True"
I have some combo boxes with code for adding new items to the source table with a form when it doesn't exist.
The code will replace Chr(47) / and Chr(92) \ with Chr(45) - if present. This is done because a file name is created using concatenation later.
The problem is if a character is replaced, I get an Access error that the item is not in the list. This does not happen if a character is not replaced. In both instances the correct items are added to the corresponding tables.
I have tried replacing the character before passing it to OpenArgs, AfterUpdate, on the form after it opens, etc. The error does not break so the program is working, I just want to eliminate a unnecessary pop-up message.
Any help is greatly appreciated.
Private Sub cboManual_NotInList(NewData As String, Response As Integer)
Dim MyMessage As String
Dim myButtons As Integer
Dim myTitle As String
Dim strSQL As String
On Error GoTo ErrHandler
MyMessage = "This Manual does not exist. Create it?"
myButtons = vbYesNo + vbDefaultButton1 + vbQuestion + vbApplicationModal
myTitle = "Add Manual?"
MyChoice = MsgBox(MyMessage, myButtons, myTitle)
If MyChoice = 6 Then
If Not DBAuthority = "Admin" And Not DBAuthority = "Data Entry" Then
Response = acDataErrContinue
MsgBox "Sorry, authorized access only", _
vbOKOnly, "Important Information"
Exit Sub
Else
Response = acDataErrAdded
CallerField = "Manual"
CallerForm = "NewDocument"
NewData = Replace(NewData, Chr(47), Chr(45))
NewData = Replace(NewData, Chr(92), Chr(45))
DoCmd.OpenForm "AddManual", windowmode:=acDialog, OpenArgs:=NewData
Me.cboManual.RowSource = Me.cboManual.RowSource
Me.cboManual.value = strAddManual
strManual = Me.cboManual.value
strAddManual = vbNullString
Me.cboSection.value = strAddSection
strSection = Me.cboSection.value
strAddSection = vbNullString
Me.cboEngine.value = strAddEngine
strEngine = Me.cboEngine.value
strAddEngine = vbNullString
End If
ElseIf MyChoice = 7 Then
Response = acDataErrContinue
MsgBox "Select Manual from list.", vbOKOnly, "Select Manual"
Me.cboManual.Undo
Me.cboManual.SetFocus
Exit Sub
End If
Exit Sub
ErrHandler:
If Err = 20 Then
Response = acDataErrContinue
ElseIf Err = 94 Then
Response = acDataErrContinue
Resume Next
ElseIf Err = 2237 Then
Response = acDataErrContinue
Resume Next
ElseIf Err = 0 Then
Response = acDataErrContinue
Else
MsgBox "cboManual.NotInList Err = " & Err.Number & " :" & Err.Description
Exit Sub
End If
Exit Sub
End Sub
Option one
Replace while typing
Select Case KeyCode
Case vbKeyDown
Me![cboNewPart].Dropdown
Case 220, 191 ' / and \
KeyCode = 189 ' with -
Case Else
End Select
Option two
after adding the new value to the table. do
me.combo.undo, me.combo.requery. me.combo.value = newValue
followed by acDataErrContinue
this way you won't get error message but the list will flicker a and it's purely a hack.
Try using a different variable name (other than NewData) to store the modified version of the value passed to the NewData argument, i.e.:
Dim NewString as String
NewString = NewData
NewString = Replace(NewString, Chr(47), Chr(45))
NewString = Replace(NewString, Chr(92), Chr(45))
DoCmd.OpenForm "AddManual", windowmode:=acDialog, OpenArgs:=NewString
Since VBA arguments are passed ByRef unless otherwise stated, any modification to the argument value will be modifying the original value passed to your cboManual_NotInList event handler.
Given the above, you could alternatively try changing the NewData argument to be passed by value (ByVal):
Private Sub cboManual_NotInList(ByVal NewData As String, Response As Integer)
In if statements I always have to use if isnull(x) or x = "" then
What is the difference between the two and why doesn't each of them work in some cases?
NULL is the absence of a value*
"" is a zero-length string
Due to implicit conversion - an object that hasn't been populated yet might return true for both cases. However if you were to place a value of "" in there, it would return False for isnull() and True for = ""
*Credit to Comintern for improving the explanation (see comments)
If you do that check, to makes sense x must be a Variant, and Null is not "Nothing". Nothing means Empty and a special check for that exists.
Run this and see:
Dim x As Variant
Debug.Print IsEmpty(x) ' True
Debug.Print IsNull(x) ' False
Debug.Print x = "" ' True
Debug.Print Nz(x) = "" ' True
x = Null
Debug.Print IsEmpty(x) ' False
Debug.Print IsNull(x) ' True
Debug.Print x = "" ' Null
Debug.Print Nz(x) = "" ' True
x = ""
Debug.Print IsEmpty(x) ' False
Debug.Print IsNull(x) ' False
Debug.Print x = "" ' True
Debug.Print Nz(x) = "" ' True
As said by the other answers, null is nothing, "" is an empty string.
To test for both null and empty variables in access VBA, use the following:
If nz([variable], "") = "" Then do stuff
The nz function converts null variables to "" or 0
when I want to check for a null/ empty string
I use
If x & "" <> "" then
isnull means the value is not known while edit: "" means it is zero-length string*
*Credit: #Macro Man.
I want to have a reset button for my form. My form contain an attachment column. When I used the code below for my form, it's work except for Me.Attachment.Value = "".
Thank you for your kind help :)
Private Sub Command10_Click()
Form.Refresh
Me.Regulation.Value = ""
Me.Link.Value = ""
Me.Link2.Value = ""
Me.Attachment.Value = ""
Me.Comment.Value = ""
Me.PIC.Value = ""
Me.Date_Stamp.Value = ""
End Sub
Try using NULL when wanting to reset a field to nothing. "" is an actual zero-length string so any field that does not accept a string as a value would fail. I would also get rid of .value everywhere.
Me.Regulation = NULL
Me.Link = NULL
Me.Link2 = NULL
Me.Attachment = NULL
Me.Comment = NULL
Me.PIC = NULL
Me.Date_Stamp = NULL
The below code is part of a program that populates a word document from an access database.
This part of the code adds a picture with the user's signature at the bookmark location 'Signature'. For some reason it works the first attempt, but the nex time it runs it pastes at the initial document's bookmark location and not the new document.
appword.ActiveDocument.Bookmarks("Signature").Select
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.InlineShapes.AddPicture FileName:= _
SASignaturePath, _
LinkToFile:=False, SaveWithDocument:=True
I've attempted to use a few other selection commands like:
Selection.Goto What:=wdGoToBookmark, Name:="Signature"
and
objWork.ActiveDocument.Bookmarks("Signature").Range.Select
with no luck.
Edit: Adding additional info as requested.
The entire function pulls some global variables from access and autopopulates a word document with them. The global variable 'SASignaturePath' has the file location of the signature image.
Below is the entire function being called when the user presses the 'create cost letter' button.
Function fillCostLetter()
Dim appword As Word.Application
Dim doc As Word.Document
Dim Path As String
TodayDate = Format(Now(), "mmmm dd, yyyy")
On Error Resume Next
Error.Clear
Set appword = GetObject(, "word.application")
If Err.Number <> 0 Then
Set appword = New Word.Application
appword.Visible = True
End If
Path = "Z:\DocFolder\ServiceAssociateToolBox\CostLetterTestStage.docx"
Set doc = appword.Documents.Open(Path, , True)
With doc
.FormFields("Date").Result = TodayDate
.FormFields("BillName").Result = BillName
.FormFields("BillAmmount").Result = BillAmmount
.FormFields("BillAddress").Result = BillAddress
.FormFields("BillAmmount").Result = BillAmmount
.FormFields("BillCity").Result = BillCity
.FormFields("BillState").Result = BillState
.FormFields("BillZip").Result = BillZip
.FormFields("SiteZip").Result = SiteZip
.FormFields("SiteState").Result = SiteState
.FormFields("SiteCity").Result = SiteCity
.FormFields("SiteStreetType").Result = SiteStreetType
.FormFields("SiteStreetName").Result = SiteStreetName
.FormFields("SiteStreetNo").Result = SiteStreetNo
.FormFields("BillName2").Result = BillName
.FormFields("WorkRequest").Result = WR_NO
.FormFields("CustName").Result = CustName
.FormFields("SAName").Result = SAName
.FormFields("SADeptartment").Result = SADept
.FormFields("SAPhone").Result = SAPhone
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
ActiveDocument.Bookmarks("Signature").Range.InlineShapes.AddPicture FileName:= _
SASignaturePath, _
LinkToFile:=False, SaveWithDocument:=True
Selection.Goto What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=1
End With
appword.Visible = True
appword.Activate
Set doc = Nothing
Set appword = Nothing
End Function
Changing and then using the selection as an insertion point is generally bad practice. What you should rather do is use the actual Range of the bookmark, which can be obtained by calling:
ActiveDocument.Bookmarks("BookmarkName").Range
The obtained Range can then be used in your above code instead of Selection, i.e.
ActiveDocument.Bookmarks("BookmarkName").Range.InlineShapes.AddPicture (...)