I've manipulated and tried a different way about going about something. I have a combo-box(Coding_drop_down) on a form(Coding Pop Up).
I previously had taken over maintenance for this database from someone else so everything was pre-existing. The combo-box itself had a Value List in it and referenced that when clicking the drop down arrow. However I've since changed that and bound it to a table that I created (for something past this). Now when I go to the drop down I get those specific values that I did before only loaded from a table rather than a Value List. The problem is that when selecting the value it does not select. Am I missing something or does the VBA need to be manipulated ???
VBA Code for the combo-box:
Private Sub Command1_click()
On Error GoTo Err_Command1_Click
Dim stDocName As String
stDocName = "Query to do easier coding"
DoCmd.OpenQuery stDocName, acNormal, acEdit
Exit_Command1_Click:
Exit Sub
Err_Command1_Click:
MsgBox Err.Description
Resume Exit_Command1_Click
End Sub
The code for the Button :
Private Sub Command7_Click()
On Error GoTo Err_Command7_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "Query to do easier coding"
DoCmd.Close acForm, stDocName
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_Command7_Click:
Exit Sub
And the on Click event of the actual Combo-box:
Private Sub Coding_drop_down_Click()
Dim test As String
test = Me.Coding_drop_down
MsgBox test
End Sub
Ok so try this out for your comboBox it might give you a good start. The items you want in the comboxBox are listed in the .addItem and they are added to the ComboBox once the form is started. When you select the item it then stores the value in the first cell of the sheet you want. If this is something that can help you out let me know and we can work towards helping with your problem.
Private Sub UserForm_Initialize()
With ComboBox1
.AddItem ("Item 1")
.AddItem ("Item 2")
.AddItem ("Item 3")
End With
End Sub
Private Sub ComboBox1_Click()
ActiveSheet.Range("A1") = ComboBox1.Value
End Sub
The issue was that the combo-box was actually bound to something and that was the reason for the issue.
Related
I am working with Access Database VBA.
I have noticed if a user has filled a few of the text boxes in and then clicks the windows close button, the form logs that into the records.
I am wondering what is the best way to prevent the form from entering the uncompleted record on close?
There were a few sites pointing to placing a code in the beforeupdate function.
This is what I have tried.
Private Sub frmRecLog_BeforeUpdate(Cancel As Integer)
DoCmd.SetWarnings False
Me.Undo
Cancel = True
Exit Sub
End Sub
This code does not work at all for me.. I tried my best, haha.
Anything helps.
So, what you need is to insert a command button Save. If user do not hit on Save then it will not save any records. They will get a warning that data is not saved. You need declare a private boolean variable and write codes to save and warning. So full code will be like below.
Option Compare Database
Option Explicit
Private blnSaveRecord As Boolean
Private Sub cmdSave_Click()
blnSaveRecord = True
DoCmd.RunCommand (acCmdSaveRecord)
blnSaveRecord = False
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
If Not blnSaveRecord Then
Cancel = True
strMsg = "Please save the record.," & _
vbNewLine & "or press ESC from keyboard to cancel the operation."
Call MsgBox(strMsg, vbInformation, "Save Record")
'Me.Undo 'You can set undo option here if you do not want to press ESC.
End If
End Sub
I have hit a wall and I am completely at a loss.
So I have a Form in MS Access. In it I have a single text box and a single button. When I type in a number in the text box and then click the button it opens a public sub which then runs a few queries, updates the database, displays a text box and then clears out the text box. This all works perfectly.
My issue is trying to do the exact same thing with hitting enter in the text box. The strangest thing is that the code works fine right after I open up the form, but all subsequent attempts give the following error until I close the form and reopen it:
Data type mismatch in criteria expression.
For the life of me I cannot figure out why it does what I want the first time, then falls apart on me.
Here is the complete VBA code for reference:
Option Compare Database
Public Sub Cut_Update()
On Error GoTo Cut_Update_Err
DoCmd.OpenQuery "UPDATE_WIP_Cut", acViewNormal, acEdit
DoCmd.OpenQuery "UPDATE_LastRun", acViewNormal, acEdit
MsgBox "Database Updated"
[Forms]![Portal_02_Cut]![WO_Num].Value = ""
Cut_Update_Exit:
Exit Sub
Cut_Update_Err:
MsgBox Error$
Resume Cut_Update_Exit
End Sub
'------------------------------------------------------------
' Return in Textbox
'
'------------------------------------------------------------
Private Sub WO_Num_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
Call Cut_Update
Me.Refresh
End If
End Sub
'------------------------------------------------------------
' Command2_Click
'
'------------------------------------------------------------
Private Sub Command2_Click()
Call Cut_Update
End Sub
The only thing you do in between the calls in the form is:
[Forms]![Portal_02_Cut]![WO_Num].Value = ""
So, try with either:
[Forms]![Portal_02_Cut]![WO_Num].Text = ""
or:
[Forms]![Portal_02_Cut]![WO_Num].Value = Null
My only guess is that the update is trying to update with a non numeric value.
Let me know
Option Compare Database
Public Sub Cut_Update()
On Error GoTo Cut_Update_Err
'' Make sure it is numeric
If IsNumeric([WO_Num].Value) Then
DoCmd.OpenQuery "UPDATE_WIP_Cut", acViewNormal, acEdit
DoCmd.OpenQuery "UPDATE_LastRun", acViewNormal, acEdit
MsgBox "Database Updated"
[Forms]![Portal_02_Cut]![WO_Num].Value = ""
End If
Cut_Update_Exit:
Exit Sub
Cut_Update_Err:
MsgBox Error$
Resume Cut_Update_Exit
End Sub
'------------------------------------------------------------ ' Return in Textbox '
'------------------------------------------------------------
Private Sub WO_Num_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
Call Cut_Update
Me.Refresh
End If
End Sub
'------------------------------------------------------------ ' Command2_Click '
'------------------------------------------------------------
Private Sub Command2_Click()
Call Cut_Update
End Sub
I'm setting up a database in MS Access 2013, and want to ask the user a yes/no if they want to save or discard their non-saved record or edit before navigating away from the current record in my Access form.
The user should not be met with the question if pressing either the "add record" or "save" buttons.
Can someone point to where my problematic code is / or what I need?
Also, I'm new to Access, so please be gentle.
I have tried a few different guides or other answers around the web, but haven't gotten exactly to where I want to be.
My code is as such (cbotxt_Change and Form_Load relate to other parts of the form)
Private blnGood As Boolean
Option Compare Database
Private Sub cbotxt_Change()
Me.txt1.Value = Me.test1.Column(2)
Me.txt2.Value = Me.test1.Column(3)
Me.txt3.Value = Me.test1.Column(4)
End Sub
Private Sub Form_Load()
DoCmd.GoToRecord , , acNewRec
End Sub
Private Sub button_addRecord_Click()
blnGood = True
DoCmd.GoToRecord , , acNewRec
blnGood = False
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
If Not blnGood Then
strMsg = "want to abort?"
If MsgBox(strMsg, vbYesNo + vbQuestion, "Yes") = vbYes Then
Me.Undo
Else
Yes = True
End If
End If
End Sub
Using the code above, "want to abort?" is asked whenever the user is attempting to navigate away from the current record + when "add record" or "save" buttons are pressed. If the user answers "No", then the entry will save and the action in question be performed, except the "add record" button, which only seems to save now, not add a new record.
Seems a little convoluted to me. Try with:
Private Sub button_addRecord_Click()
DoCmd.GoToRecord , , acNewRec
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
strMsg = "want to abort?"
If MsgBox(strMsg, vbYesNo + vbQuestion, "New Entry") = vbYes Then
Cancel = True
Me.Undo ' or let the user press Escape.
End If
End Sub
I have moderate knowledge in SQL and able to manage simple queries...
Basic intention was to add a drop-down on a Excel macro I created (instead of a text box where user give whatever values he wants) but after so much research I came to know that only a Userform can help me to satisfy my requirement, Now I want to know how a value selected under a Userform, can be made available under a Module, so that I can use those values selected from a drop down for any other purpose. I have created a sample user-form with below codes
For USER-FORM
Private Sub cmdAdd_Click()
Call Try1
Unload Me
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "Please use the Close Form button!"
End If
End Sub
Private Sub UserForm_Initialize()
Dim cPart As Range
Dim cLoc1 As Range
Dim ws As Worksheet
Set ws = Worksheets("LookupLists")
For Each cPart In ws.Range("PartIDList")
With Me.cboPart
.AddItem cPart.Value
.List(.ListCount - 1, 1) = cPart.Offset(0, 1).Value
End With
Next cPart
For Each cLoc1 In ws.Range("LocationList")
With Me.cboLocation
.AddItem "Cluster1"
.AddItem "Cluster2"
.AddItem "Location 3"
End With
Next cLoc1
Me.txtDate.Value = Format(Date, "Medium Date")
Me.txtQty.Value = 1
Me.cboPart.SetFocus
End Sub
For Module
Sub Rectangle1_Click()
frmPartLoc.Show
End Sub
Sub Try1()
Dim Location
Location = cboLocation.Value
Part = cboPart.Text
MsgBox Location.Value
MsgBox Part.Text
End Sub
actually this code have some issue here
"Location = cboLocation.Value Part = cboPart.Text"
I guess its possible to get the values on a MODULE that we selected using a USER-FORM. But I am having some mistake with my code, or understand this stuff little differently. Please help me to correct my code.
I take help from a website to create this User form (not using the complete code) if you want to please use this link to get the zip file of the same or you can browse there using below link
http://www.contextures.com/xlUserForm01.html
Thanks in advance for helping me.
Thanks to Tigeravatar who help to find the solution.
We have to mention the USER-FORM name as well prior to combobox name from which the vale we are populating from USER-FORM.
Please find the Corrected code below
Sub Try1()
Dim Location
Location = frmPartLoc.cboLocation.Value
Part = frmPartLoc.cboPart.Value
MsgBox Location
MsgBox Part
End Sub
Thanks again for all the comments
I got macro below which fires twice (showing same MessageBox twice). First when ComboBox1 opens and second when ComboBox1 closes.
Private Sub ComboBox1_DropButtonClick()
If Me.ComboBox2.Text = "" Then
MsgBox "Fill text box"
Else
'Do stuff
End If
End Sub
Is there any way to make it show MessageBox once. I want user to select the value in ComboBox1 first before clicking on ComboBox2 DropButton.
Here is a very unelegant work-around using a "count" variable that prompts the MsgBox only the first and not the second time.
Dim count As Integer
Private Sub ComboBox1_DropButtonClick()
count = count + 1
If Me.ComboBox2.Text = "" Then
If count = 1 Then
MsgBox "Fill text box"
Else
count = 0
End If
Else
'Do stuff
End If
End Sub
However, I highly suggest to use the ComboBox1_Change() event if it's not necessary to use the drop button one.
P.S.: the declaration of the "count" variable needs to stay out of the method. This is due to the fact that:
if it stays inside, it's a local variable of the method and so loses its modifications every time the method is ended;
if it stays outside, it will keep the modifications even once the method has ended its run.
I would do it using the combobox_enter event, but this only checks when the focus is switched
Private Sub ComboBox1_Change()
If ComboBox1.Text = "" Then
ComboBox2.ShowDropButtonWhen = fmShowDropButtonWhenNever
Else
ComboBox2.ShowDropButtonWhen = fmShowDropButtonWhenAlways
End If
End Sub
Private Sub ComboBox2_Enter()
If ComboBox1.Text = "" Then
MsgBox "Must first set value to combobox1"
ComboBox1.SetFocus
End If
End Sub
Private Sub UserForm_Initialize()
ComboBox1.AddItem "None", 0
ComboBox1.AddItem "Select Me", 1
ComboBox2.AddItem "None", 0
ComboBox2.AddItem "Select Me", 1
ComboBox2.ShowDropButtonWhen = fmShowDropButtonWhenNever
End Sub
My code does some extra things that I just think look pretty, you really only need the _Enter function