ms access how to reset attachment in form (clear) using button? - ms-access

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

Related

Docmd.Findrecord finds in textbox

I'm using the docmd.Findrecord to find a record based on a value the user enter in a textbox. The problem is that Findrecord considers the textbox as a field and just keep finding the string in the field and not the records in the table I tricked the command by deleting the string in the textbox just before the search and replacing it back after the search. How can I fix this without this extra trick ? Here is the code I use:
Private Sub SearchRecord_Click()
Dim strwhat2find As String
If SearchTxt.Value = "" Then
Else
strwhat2find = SearchTxt.Value
SearchTxt.Value = ""
DoCmd.FindRecord strwhat2find, acAnywhere, False, acSearchAll, True, acAll, True
SearchTxt.Value = strwhat2find
End If
End Sub
End Sub

Access Run time error - '-2147352567

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?

Viewing combobox values from textbox values Microsoft Access 2007

I have a form ("Patient Complications") where users input data to a form using 2 cascading combo boxes ("catcombo" and "speccombo"). The combo boxes pull their values from a table ("Complications"). The table has a field for complication category (for example, infection, bleeding, mechanical). A second field lists specific complications (for example, if the complication category is "bleeding", the specific complication could be "GI" or "other"). The input from the combo boxes are concatenated and put into a text field on the form ("Complication"). That part works fine.
My form has several command buttons, including "edit" and "save" command buttons. Since I don't want users interacting with the "complication" field on the form, I have the field become not visible when the "edit" button is clicked. Instead, the 2 combo boxes become visible and allow users to input data. When "save" is selected, the reverse occurs. The two combo boxes become not visible, and the complication field becomes visible and locked.
Unfortunately, when "edit" is selected, the combo boxes are visible but show up blank (nothing is selected or displayed). I am trying to have the boxes display the inputs that were given to the text field. For example, if the text field displays "Bleeding, Other", I want the catcombo box to display "Bleeding" and the speccombo box display "Other". I have been unable to find anything to this effect. If anyone has any ideas it would be greatly appreciated.
The relevant code is included below. Please let me know if I can provide further clarification.
Private Sub catcombo_AfterUpdate()
Me.speccombo.Requery
End Sub
Private Sub speccombo_OnCurrent()
Dim strsql As String
strsql = "SELECT [Complications]![Specific Complication] FROM tblComplications" & _
"WHERE [Complication Category]=Forms![Patient Complications]![catcombo].value"
End Sub
Private Sub speccombo_AfterUpdate()
Forms![Patient Complications]![Complication] = Me.catcombo.Value & ", " & Me.speccombo.Value
End Sub
Private Sub save_Click()
Me.recordcount.Caption = "Record " & Me.CurrentRecord & " of " & Me.Recordset.recordcount
Me.Patient_Initials.Visible = False
Date_of_Complication.Locked = True
Complication.Visible = True
Complication.Locked = True
comments.Locked = True
catcombo.Visible = False
speccombo.Visible = False
Me.edit.Visible = True
Me.edit.SetFocus
Me.help.Visible = False
Me.save.Visible = False
Me.first.Visible = True
Me.next.Visible = True
Me.previous.Visible = True
Me.last.Visible = True
Me.addnew.Visible = True
Me.close.Visible = True
Me.cancel.Visible = False
End Sub
Private Sub edit_Click()
Me.recordcount.Caption = "Record " & Me.CurrentRecord & " of " & Me.Recordset.recordcount
Me.Patient_Initials.Visible = False
Date_of_Complication.Locked = False
Complication.Visible = False
comments.Locked = False
catcombo.Visible = True
catcombo.Locked = False
catcombo.Enabled = True
speccombo.Visible = True
speccombo.Locked = False
speccombo.Enabled = True
Me.cancel.Visible = True
Me.cancel.SetFocus
Me.edit.Visible = False
Me.help.Visible = True
Me.save.Visible = True
Me.first.Visible = False
Me.next.Visible = False
Me.previous.Visible = False
Me.last.Visible = False
Me.addnew.Visible = False
Me.close.Visible = False
End Sub
I figured it out. I added a field to the "Complications" table called "Input". This field contains the concatenated values that were put into the patient record (in the example above, the input field would be "Bleeding, Other"). The values in the Input field are the exact values that would be recorded in the "Complication" field on the Patient Complications form. I added the vba code below to the "Edit" command button code.
If Not IsNull(Forms![Patient Complications]![Complication]) Then
Dim comptext As String
Dim spectext As String
comptext = DLookup("[Complication Category]", "Complications", "Input = Forms![Patient Complications]![Complication]")
catcombo.Value = comptext
spectext = DLookup("[Specific Complication]", "Complications", "Input=Forms![Patient Complications]![Complication]")
speccombo.Value = spectext
End If
That solved my initial problem, but I then had an issue with the speccombo box displaying the last value it was given. For example, if speccombo.value="GI" when I click "edit", it would continue to display "GI" until another selection was made. This isn't a huge deal, just inconvenient. I wanted to make the speccombo box essentially zero out if catcombo was changed. I added the code below to fix that problem.
Private Sub catcombo_AfterUpdate()
Me.speccombo.Requery
Me.speccombo.Value = Null
End Sub
Please let me know if I need to provide clarification for anything.

Using Passed Variable to Select Items like "Me.myvar.Item"

I am trying to create a new function that I can pass variables to and those variables will be used to select and modify items.
Function
Function TabDisplay(Switch As String, TargetTab As String)
If (Me. & Switch & .Value) = -1 Then
Me.Tabs.Pages.Item("TargetTab").Visible = True
ElseIf Me.Switch.Value = 0 Then
Me.Tabs.Pages.Item(TargetTab).Visible = False
Else
Me.Switch.Value = "0"
End If
End Function
How I call the function
TabDisplay "SoftwareInstallable", "tabSoftware"
Working Code that I used before attempting this function
If Me.SoftwareInstallable.Value = -1 Then
Me.Tabs.Pages.Item("tabSoftware").Visible = True
ElseIf Me.SoftwareInstallable.Value = 0 Then
Me.Tabs.Pages.Item("tabSoftware").Visible = False
Else
Me.SoftwareInstallable.Value = "0"
End If
====Final Code====
Function
Function TabDisplay(Switch As String, TargetTab As String)
If Me.Controls(Switch).Value = -1 Then
Me.Tabs.Pages.Item(TargetTab).Visible = True
ElseIf Me.Controls(Switch).Value = 0 Then
Me.Tabs.Pages.Item(TargetTab).Visible = False
Else
Me.Controls(Switch).Value = "0"
End If
End Function
Function Call
TabDisplay "SoftwareInstallable", "tabSoftware"
I think I understand what you want. But let me describe it with a different example.
I have a form with a text box named txtFoo. So I can Debug.Print its value to the Immediate window like this ...
Debug.Print Me!txtFoo.Value
Later I decide I want to store the control name in a variable so that I can Debug.Print the value of any other control simply by changing the variable.
Dim strControlName As String
strControlName = "txtBar"
Then I can use that variable to reference the matching item in the form's Controls collection, and obtain its value.
Debug.Print Me.Controls(strControlName).Value
I think you can use that approach to accomplish what you want, but I won't attempt to rewrite your code sample because I suspect I would get it wrong.

Proper way to check if an unbound control has a value

Simple scenario: a form and one text box (unbound), Text1.
If "" <> Text1 Then
MsgBox "Not Empty"
End If
The above code works. The expression ""<> Text1 evaluates to True if the text box contain characters.
The opposite doesn't work, regardless of the text box is empty or not:
If "" = Text1 Then ' or alternatively, False = ("" <> Text1)
MsgBox "Empty!"
End If
Can you clarify this issue?
The textbox is usually null if it does not contain anything, this is not the same as a zero length string (""). It can often be best to use:
If Trim(Text1 & "") = vbNullString
'Empty
This will be true if the textbox contains spaces, a zero length string or null.
Alternatively, I've got a small function that I find useful for checking that sort of thing when dealing with various variable types and I just want to know whether it's blank.
'-----------------------------------------------------------------------------'
' True if the argument is Nothing, Null, Empty, Missing or an empty string. '
'-----------------------------------------------------------------------------'
Public Function IsBlank(arg As Variant) As Boolean
Select Case VarType(arg)
Case vbEmpty
IsBlank = True
Case vbNull
IsBlank = True
Case vbString
IsBlank = (arg = vbNullString)
Case vbObject
IsBlank = (arg Is Nothing)
Case Else
IsBlank = IsMissing(arg)
End Select
End Function
Just Made a blog post today about it too.
I always use the function Nz (variable, valueIfNull) to check those kind of things. For example:
if (Len(Nz(Me.txt1, "")) > 0) then
'Me.txt1 does not contain a value
end if
Or if I want to save a textbox's value to a recordset:
rst!Name = Nz(Me.txtName, "No name given")