I'd like to make "Any Part of Field" Default (Access VBA) - vba

In a simple Find action dialog box on an MSAccess (2007) form I want to make "Any Part of Field" the default value when the Find and Replace box appears.
The actual default value is "whole field". I though that I could change that with the following line:
DoCmd.FindRecord " ", acAnywhere, , , , , False
But that doesn't make any difference. The rest of the code works fine (associated with a command button). But that above line does nothing whether it's there or not. Please help. I have the following code:
Private Sub AppNAppFind_Click()
On Error GoTo AppNAppFind_Click_Err
On Error Resume Next
Err.Clear
DoCmd.FindRecord " ", acAnywhere, , , , , False
DoCmd.RunCommand acCmdFind
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If
AppNAppFind_Click_Exit:
Exit Sub
AppNAppFind_Click_Err:
MsgBox Error$
Resume AppNAppFind_Click_Exit
I'm using Access 2007.

The following code works for me in Access 2010.
Private Sub myFind_Click()
DoCmd.GoToControl "=[Screen].[PreviousControl].[Name]"
DoCmd.FindRecord " ", acAnywhere, False, acSearchAll, False, acCurrent, False
DoCmd.RunCommand acCmdFind
End Sub
I verified that the Access default for "Find" is "Whole Field" on this machine, yet when I click my button the Find dialog has "Any Part of Field" selected for "Match".

I'm not sure if this is an option in Access 2007, but in Access 2016, you can go to File > Options > Client Settings and look for Default find/replace behaviour and change it from Fast search to General search and it will set the default find settings to Look in: Current document and match in Any Part of Field.
This setting is for the client and will be effective in any access db you open and is persistent.

Related

Automation to verify user form is open and if not, close Excel using CATIA VBA

I'm working on a project to detect if a user form is open and if not, close an excel program hidden in the background. The issue I'm having is the if statement in the Function Excel_Form_Close. The if statement works independently and does what it should do but when moved into the function area, the msgbox "Entered" triggers but the If statements do not. Why would the If statement be getting skipped during the load?
Sub Macro_Timer()
CATIA.Application.OnTime Now + TimeValue("00:00:10"), "Excel_Form_Close"
End Sub
Function Excel_Form_Close()
MsgBox "Entered Excel_Form_Close"
If Currentproject.Allforms("USER_FORM").IsLoaded = False Then
Call APP_CLOSE(LIST_APP, LIST_FILE)
MsgBox "Excel Closed"
End
End If
If Currentproject.Allforms("USER_FORM").IsLoaded = True Then
MsgBox "USER FORM CURRENTLY LOADED"
Call Macro_Timer
End If
End Function
maybe bcs the end function you have on the first if statement completely shutdowns your code if no necessity so ever, you could also add only one if statement in ur function after all there is only two possibilities. Here is the possible fix.
Function Excel_Form_Close()
MsgBox "Entered Excel_Form_Close"
If Currentproject.Allforms("USER_FORM").IsLoaded = False Then
Call APP_CLOSE(LIST_APP, LIST_FILE)
MsgBox "Excel Closed"
else
MsgBox "USER FORM CURRENTLY LOADED"
Call Macro_Timer
End If

How to make to click in button and open form releated in value in combox in Ms Access?

I open a form as shown here: How to use combobox to open specific form in Ms Access
But I still have a problem. When I click the button, it opens two forms immediately. What I want to do, when the value in the combobox is 1, is to open only form 1, and when the value in the combobox is 2, open form 2.
Should I correct something in my macro? How do I do it?
I personally don't like this macro builder.
Here's a proper way to do it with VBA.
Select your combobox and go in the events. In the After Update event, remove your macro code and add VBA code instead, using the option "Code Builder"
Your code should be this :
Private Sub ComboBoxColor_AfterUpdate()
On Error GoTo Err_Handler
Dim strForm As String
Select Case ComboBoxColor.Value
Case "Color1"
strForm = "Form1"
Case "Color2"
strForm = "Form2"
Case "Color3"
strForm = "Form3"
Case Else
MsgBox "I don't know what to do with this combobox value"
GoTo Exit_Sub
End Select
DoCmd.OpenForm strForm, acNormal
Exit_Sub:
Exit Sub
Err_Handler:
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Sub
End Sub
Adapt ComboBoxColor with the proper name, and Form1/Form2/Form3 with the names of your forms of course (and the Color1/Color2/color3 if needed)

Checking if a TextBox password field does not have any value or empty in Access

How will I know if a password textbox field in access is Empty or there's no any value on it?
Private Sub cmdLogin_Click()
If IsNull(Me.txtPassword.Value) Or Trim(Me.txtUserName & "") = vbNullString Then
MsgBox "Username or Password must not be empty.", vbCritical, "Error!"
Else
MsgBox "welcome"
End If
End Sub
The code is what I've tried and even though I tried using Len(Me.txtPassword & "") = 0, it still executes the if statement. My concern is that if the textbox fields' input mask is a password, it should accept the space(by pressing spacebar) as a text or part of the string. Is there any way I can figure this out?
You are specifically not allowing a blank space as a password in your original if statement. This should work:
Private Sub cmdLogin_Click()
If IsNull(Me.txtPassword.Value) Then
MsgBox "Username or Password must not be empty.", vbCritical, "Error!"
Else
MsgBox "welcome"
End If
End Sub
I think it is irrelevant though. Surely what matters is that the password matches the one stored in the database? Personally, I would set the password field in the table to Required = True and AllowZeroLength = False.
In MS Access it is very hard to update a field value to something ending " ". In datasheet view on the table and on forms, when a text field is updated, the value is automatically trimmed. It's quite annoying.

.CanCheckOut not returning the correct value?

I am trying to check if a file, stored on a SharePoint server, can be checked out or not so I can alert the user and stop them from editing the workbook without checking out the file. I have tried the code below but when I try to run it, it believes it is not possible to check out and doesn't do anything. Yet when I check the SharePoint server it says that it is not checked out and can be checked out.
Function CheckOutCheck2() As Boolean
'CheckOutCheck2 = True
If Workbooks.CanCheckOut("FilePath") = True Then
MsgBox "Please make sure the workbook is checkout before making any changes", vbInformation, "Check Out"
CheckOutCheck2 = True
Debug.Print ("Can Check Out")
End If
If Workbooks.CanCheckOut("Filepath") = False Then
CheckOutCheck2 = False
Debug.Print ("Cant Check Out")
End If
End Function
and when I click my button I do this:
If CheckOutCheck2() = True Then
Exit Sub
Else
CreateNew.Show
End If
I want to be able to stop the user from doing anything until the file is checked out.
Anybody have any idea why this might be happening?

How do I get a value indicating whether Recordset.Find found anything?

I wrote a program that would add, delete, save and search through records in a database (recordset). However I was doing it in a team. My task was to add the search function to the program, which I have; however I am having problems with adding an error message for when somebody types a word/anything that isn't in the database/recordset.
So for example in the textbox(txtFindBox.Text) if they type "ashbndash" it would come up with an error message. I've commented out my own error message boxes but tell me where I'm going wrong please :(
Here's the code for the find button.
Private Sub cmdFindDB_Click()
adoCustomer.Recordset.MoveFirst
If optLastName.Value = True Then
adoCustomer.Recordset.Find "LastName='" & txtFindBox.Text & "'"
'Else
'MsgBox ("NO RECORD FOUND")
End If
If OptFirstName.Value = True Then
adoCustomer.Recordset.Find "FirstName='" & txtFindBox.Text & "'"
'Else
'MsgBox ("NO RECORD FOUND")
End If
End Sub
edit: Just like to say the problem is that every time I hit the button 'find' it comes up with the message "NO RECORD FOUND" in a msgbox even though it finds the answer, it also comes up with that msgbox if you type in gibberish too.
Thanks for your time
Regards Haroon
Here is an example of how to do what you are trying to do: METHOD: Recordset::Find
Example for your code:
adoCustomer.Recordset.MoveFirst
adoCustomer.Recordset.Find "LastName='" & txtFindBox.Text & "'"
If (adoCustomer.Recordset.BOF = True) OR (adoCustomer.Recordset.EOF = True) Then
MsgBox "Record not found"
End If
Instead of checking that the Value property is true, you need to check EOF and BOF. These stand for End Of File and Beginning Of File. So if either is true, then you are not "inside" the recordset, meaning you did not find anything.