I'm new to forms in MS access and I've created a form with a combo box that auto fills a few things i'm looking for, basically and name, phone #, and check out date. I have added another text box for "check in date" and I'm able to input the date but it will update the first record in the table that I'm pulling information from and not the record that the auto fill combo box displays. would anyone know a fix to update the record that the auto fill display versus the top record of the table?
Private Sub Combo0_Change()
Me.txtfname = Me.Combo0.Column(1)
Me.txtlname = Me.Combo0.Column(2)
Me.txtphone = Me.Combo0.Column(3)
Me.txtpump = Me.Combo0.Column(4)
Me.txtdateissue = Me.Combo0.Column(5)
Me.txtduedate = Me.Combo0.Column(6)
Me.txtCheckInDate = Me.Combo0.Column(7)
End Sub
Private Sub Combo0_Click()
End Sub
Private Sub txtCheckInDate_Change()
End Sub
get the source of Combo0 combobox, then in Private Sub txtCheckInDate_Change() function, change its source to that source + your filter, like
Me.Combo0.RowSource = "[Existing Combo Source SQL]" & _
" WHERE [YourDateField] = #" & me.txtCheckInDate & "#"
Related
I am using draw to mark up a pdf format index map. So in grid 99, the text hyperlinks to map99.pdf
There are 1000's of grid cells - is there a way for a (macro) to scan for text in a sheet that is like
Text in File | Link to add
99|file:///c:/maps/map99.pdf
100|file:///c:/maps/map100.pdf
and add links to the relevant file whenever the text is found (99,100 etc).
I don't use libre much but happy to implement any programatic solution.
Ok, after using xray to drill through enumerated content, I finally have the answer. The code needs to create a text field using a cursor. Here is a complete working solution:
Sub AddLinks
Dim oDocument As Object
Dim vDescriptor, vFound
Dim numText As String, tryNumText As Integer
Dim oDrawPages, oDrawPage
Dim oField, oCurs
Dim numChanged As Integer
oDocument = ThisComponent
oDrawPages = oDocument.getDrawPages()
oDrawPage = oDrawPages.getByIndex(0)
numChanged = 0
For tryNumText = 1 to 1000
vDescriptor = oDrawPage.createSearchDescriptor
With vDescriptor
'.SearchString = "[:digit:]+" 'Patterns work in search box but not here?
.SearchString = tryNumText
End With
vFound = oDrawPage.findFirst(vDescriptor)
If Not IsNull(vFound) Then
numText = vFound.getString()
oField = ThisComponent.createInstance("com.sun.star.text.TextField.URL")
oField.Representation = numText
oField.URL = numText & ".pdf"
vFound.setString("")
oCurs = vFound.getText().createTextCursorByRange(vFound)
oCurs.getText().insertTextContent(oCurs, oField, False)
numChanged = numChanged + 1
End If
Next tryNumText
MsgBox("Added " & numChanged & " links.")
End Sub
To save relative links, go to File -> Export as PDF -> Links and check Export URLs relative to file system.
I uploaded an example file here that works. For some reason your example file is hanging on my system -- maybe it's too large.
Replacing text with links is much easier in Writer than in Draw. However Writer does not open PDF files.
There is some related code at https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=1401.
I've created a form within Access which uses a cross-tab query as its data source.
The column headings for the query are 1, 2, 3, 4 and 5 representing week numbers.
The values display items such as 3/3 = 100.00% or 0/13 = 0.00% or 3/14 = 21.00%.
I've added conditional formatting to the text boxes on the form.
Expression Is Right([2],7)="100.00%" works and displays the figure in bold red when the percentage is 100.
Expression is Val(Right([2],7))=100 also works - converting the text value to a numeric value.
The problem I'm having is that I'm not always looking for 100% - it depends on the value within a table. What I'm trying to do is
Val(Right([2],7))=(SELECT ParamValue*100 FROM tbl_System WHERE Param='SampleSize') - this doesn't work.
Neither does:
Eval(Val(Right([2],7))=(SELECT ParamValue*100 FROM tbl_System WHERE Param='SampleSize'))
or
Val(Right([2],7))=EVAL(SELECT ParamValue*100 FROM tbl_System WHERE Param='SampleSize')
or
Val(Right([2],7))=DLookUp("ParamValue","tbl_System","Param= 'SampleSize'")*100
or
Val(Right([2],7))=Eval(DLookUp("ParamValue","tbl_System","Param= 'SampleSize'")*100)
The SQL for the cross-tab query is:
TRANSFORM NZ(Sum(Abs([Include])),0) & "/" & NZ(Count(*),0) & " = " &
FormatPercent(NZ(Round(Sum(Abs(Include))/Count(*),2),0),2)
SELECT tbl_TMP_PrimaryDataSelection.TeamMember
FROM tbl_TMP_PrimaryDataSelection
GROUP BY tbl_TMP_PrimaryDataSelection.TeamMember
PIVOT tbl_TMP_PrimaryDataSelection.WeekNum In (1,2,3,4,5)
I don't think you can use a function in there, be it system or user-defined.
But you can define the FormatCondition dynamically at runtime, like this:
Dim txtFld As TextBox
Dim objFrc As FormatCondition
Dim strExpr As String
Set txtFld = Me!myTextBox
' Remove existing FormatConditions
txtFld.FormatConditions.Delete
' The dynamic expression
strExpr = "Val(Right([2],7))=" & DLookUp("ParamValue","tbl_System","Param='SampleSize'")*100
' Assign a new FormatCondition to text box
Set objFrc = txtFld.FormatConditions.Add(acExpression, , strExpr)
' Set the format
objFrc.ForeColor = &HFF0000
This example simply removes and recreates all FormatConditions. If you have a fixed number of conditions, you can also use the FormatCondition.Modify method (see online help).
Edit:
The final code I have used executes on the Form_Load event and adds a format to each of the five weekly text boxes:
Private Sub Form_Load()
Dim aTxtBox(1 To 5) As TextBox
Dim x As Long
Dim oFrc As FormatCondition
Dim sExpr As String
With Me
Set aTxtBox(1) = .Wk1
Set aTxtBox(2) = .Wk2
Set aTxtBox(3) = .Wk3
Set aTxtBox(4) = .Wk4
Set aTxtBox(5) = .Wk5
For x = 1 To 5
aTxtBox(x).FormatConditions.Delete
sExpr = "Val(Right([" & x & "],7))>=" & DLookup("ParamValue", "tbl_System", "Param='SampleSize'") * 100
Set oFrc = aTxtBox(x).FormatConditions.Add(acExpression, , sExpr)
oFrc.ForeColor = RGB(255, 0, 0)
Next x
End With
End Sub
Edit 2
Yes, defining FormatConditions via VBA is especially useful when dealing with multiple controls in a loop. You can do this in Design View too and save the FormatConditions permanently, simply to avoid going through the FormatConditions dialogs one by one. Or if the customer later decides that he'd rather have a different color. :)
Note: You could use Set aTxtBox(x) = Me("Wk" & x) in the loop. But actually you don't need multiple TextBox variables, you can simply re-use it.
I have a textbox and listbox, one is for entering a new topic in a help form while the other looks up those new topics. I would like to be able to present the finished topics in both the textbox and listbox simultaneously to edit or lookup as well write new records in the help form. The listbox provides functionality to view which records there are now.
I find if I put nothing in and I go to a new record the prev/next buttons will stop working, maybe there is a control I need to add to keep it from freezing or to refresh? Normally I press esc to get out of a new record edit and return to others but that does not work as usual.
Or how else may I point to the listbox's current record source?
I currently have this code:
Private Sub List35_AfterUpdate()
DoCmd.GoToRecord acDataForm, "Help Form_Editor2", acGoTo, Me.List35.ListIndex + 1
Me.List35 = Me.List35.Column(0, Form.CurrentRecord - 1)
Dim index As Integer
index = Form.CurrentRecord - 1
Me.Text53 = Me.List35.Column(Me.List35.ListIndex + 1, index)
End Sub
I keep getting some of the items to read but others are null. I have about 8 items in the source table... what is going wrong? Why would there be nulls?
Another issue after getting this updated. When the code is setup the recordset starts at new when I allow additions and edits on the form. The code displays the list item as it should but the other items will not activate from the requeried listbox item. What might correct this issue?
Private Sub List35_AfterUpdate()
Dim myTitle As String
With Me.List35
If .ListIndex > -1 Then
'Use this one if you are using bound column
myTitle = .Column(1, Form.CurrentRecord)
'use this if you want something other than the bound column
'and you have more than one column in the list (hidden or not)
'nId = .Column(1, .ListIndex)
Me.RecordSource = "SELECT * FROM FormsHelpTable WHERE HelpTitle = '" & myTitle & "'"
Me.Text53.Value = myTitle
Else
Me.RecordSource = "SELECT * FROM FormsHelpTable WHERE HelpTitle IS NULL"
'Me.Text53.Value = "(New)"
End If
End With
Me.Requery
End Sub
This checks for ListIndex. It will be -1 if you don't have anything selected.
Private Sub List35_AfterUpdate()
Dim index As Integer
With Me.List35
If .ListIndex > -1 Then
DoCmd.GoToRecord acDataForm, "Help Form_Editor2", acGoTo, .ListIndex + 1
.Value = .Column(0, Form.CurrentRecord - 1)
index = Form.CurrentRecord - 1
Me.Text53 = .Column(.ListIndex + 1, index)
End If
End With
End Sub
I'm not sure what all your code is trying to do, so I didn't make any other adjustments other than to reduce all references to List35 to a single With statement.
I normally do something like this:
Private Sub List35_AfterUpdate()
Dim nId As Long
With Me.List35
If .ListIndex > -1 Then
'Use this one if you are using bound column
nId = .Value
'use this if you want something other than the bound column
'and you have more than one column in the list (hidden or not)
'nId = .Column(1, .ListIndex)
Me.RecordSource = "SELECT * FROM TableName WHERE Id = " & nId
Else
Me.RecordSource = "SELECT * FROM TableName WHERE Id IS NULL"
End If
End With
Me.Requery
End Sub
I have created a User Form in VBA so that call centre staff can submit their numbers to our tracking spreadsheet at the end of each day. In its current design, the form successfully submits data to another sheet in the workbook. As I discovered, macro-enabled spreadsheets can't be shared (each staff member will submit at roughly 4pm), so I am looking at making a copy of the user form spreadsheet for each staff member (around 15) and directing it to submit to a shared spreadsheet every day.
i.e. 15 or so staff members use "User Form.xlsm" to submit to "Tracking Spreadsheet.xlsx" all around 4pm each day.
Q1: Do I need to make the "Tracking Spreadsheet.xlsx" a Shared workbook in case more than one staff member submits their end of day form at once?
Q2: Do I need to insert VBA code in "User Form.xlsm" that actively opens "Tracking Spreadhseet.xlsx" or can I just reference "Tracking Spreadhseet.xlsx"?
Q3: Where have I gone wrong in the code below? I'm new to VBA. I have structured my code for the submission button as follows, but it just adds data to the Daily_Tracking_Dataset sheet in the current workbook, rather than the new one:
First, I tried to change the workbook,
then i make the relevant sheet in the workbook active,
then I determine the first empty row,
then I transfer the information from the form's textboxes to the new workbook.
Private Sub Button_Submit_Click()
'Change Workbook
Dim nwb As Workbook
Set nwb = Workbooks.Open("G:\Tracking Spreadsheet.xlsx")
Dim emptyRow As Long
'Make Daily_Tracking_Dataset active
Daily_Tracking_Dataset.Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
'Transfer Information
Cells(emptyRow, 1).Value = TextBox1.Value
Cells(emptyRow, 2).Value = lstName.Value
Cells(emptyRow, 3).Value = txtROIT.Value
Cells(emptyRow, 4).Value = txtROISub.Value
Cells(emptyRow, 5).Value = txtRefsT.Value
Cells(emptyRow, 6).Value = txtRefsC.Value
Cells(emptyRow, 7).Value = txtRefsSub.Value
Cells(emptyRow, 8).Value = txtReSubT.Value
Cells(emptyRow, 9).Value = txtReSubSub.Value
End Sub
Try this sample below:
Private Sub TextBox1_afterupdate()
Dim pro As Workbook
Set pro = Workbooks.Open("F:\DOCUMENTS\Proration.xlsm")
Workbooks("proration").Sheets("sheet1").Range("i20").End(xlUp).Offset(1, 0).Value = UserForm1.TextBox1.Value
pro.Save
pro.Close True
End Sub
Regarding Q1/Q2: yes, you will need to add code to open the worksheet, and it may be better to open it Shared, at least if you do not save and close the tracking spreadsheet file immediately after inserting the data.
Did you try an Access database or something similar, where you can more easily add the required information and do not need to worry about concurrent accesses to the data "sheet"?
Regarding Q3: you did not state what is going wrong with your code at the moment.
Edit:
Regarding Q3: Try using something like nwb.Sheets( "daily_tracking_dataset" ).Cells(emptyRow, 1).Value = TextBox1.Value and be aware that emptyRow also needs to be determined using nwb, e.g. using a combination of Offset and Move(xlDown) (see Excel: Move selection to next blank row in specific column, and enumerate selection by date and type).
Thanks for the help. I ended up using the emptyrow method below:
'Begin Transfer Information and Change Workbook
Dim nwb As Workbook
Set nwb = Workbooks.Open("G:\Time To Complete Dataset.xlsx")
'Determine emptyRow
Dim emptyRow As Long
emptyRow = WorksheetFunction.CountA(nwb.Sheets("daily_tracking_dataset").Range("A:A")) + 1
'Transfer Information
With nwb.Sheets("daily_tracking_dataset")
'Datebox
.Cells(emptyRow, 1).Value = CDate(txtDate.Text)
'Listbox
.Cells(emptyRow, 2).Value = lbName.List(lbName.ListIndex)
'Textbox
.Cells(emptyRow, 3).Value = txtROT.Value
End With
ActiveWorkbook.Save
ActiveWindow.Close
End Sub
I am extremely new to all of this, and whilst I have tried searching I cant find anything that has helped me achieve what I am after.
I have a form in VB with the following:
1 x tabcontrol
10 x checkboxes which sit in various tabs on the tab control
1 x listbox
When i tick any of the check boxes, I want their text to be added to the listbox, and when I untick, their text to be taken from the listbox.
I can achieve this very easily using if statements for the changedcheck event for each checkbox but I have to do that for every single checkbox which isn't very efficient as potentially i could have 20,30 40+ check boxes. Plus if I add one at a later stage I would have to remember to add its code.
Ideally i want a method that's says: check all the checkboxes in tabcontrol if there value is true write their text to a string, if there value is false, take there text from the string. put the string in the listbox.
I started with something like this...
Dim chk As CheckBox
Dim txt As String = ""
For Each chk In TabControl1.Controls
If chk.Checked = True Then
txt = txt + chk.Text +vbCrLF
Else
txt = replace(txt, chk.text + vbCrLf, "")
End If
Next
End Sub
First problem is that the above obviously doesn't work! so any guidance there is appreciated - i put it together from reading scraps from other code.
Second problem is, i can't get my head round how the list box will be updated, as previously i was using the CheckedChanged event for each control, which if i do what i want, then there wont be a specific CheckedChanged event, as it could be any of the checkboxes (hopefully that makes sense!). I don't want to have to press a button to add the checked checkboxes to the listbox, i want it to be dynamic
any help is very much appreciated.
For your first problem add
Dim chk As Control
Dim txt As String = ""
For Each chk In TabControl1.Controls
If TypeOf chk Is CheckBox
If DirectCast(chk, CheckBox).Checked = True Then
txt = txt + chk.Text +vbCrLF
Else
txt = replace(txt, chk.text + vbCrLf, "")
End If
End If
Next
End Sub
For your second problem in CheckedChanged event you can do something like this:
Private Sub OnCheckedChanged(sender as Object, e as EventArgs) _
Handles CheckBox1.CheckedChanged
Dim chk As CheckBox = TryCast(s, CheckBox)
Dim txt as string
If c.Checked = True Then
txt = chk.Text
EndIf
End Sub