Use a string as property name - ms-access

I am trying to set values into text boxes by referring to them as strings
The text box names all follow a format:
eg. txt_N1 or txt_N2
I want to be able to go through the text boxes by incrementing the integer of the text box name, which would look like "txt_N" & CStr(intRow).
I am not sure how I am meant to go about doing this.
Below is what I've got so far
intRow = 1 'The 1st Row
recVat.MoveFirst 'Go to the 1st record in the recordset
'Loop through the records
Do Until intRow = 4 Or recVat.EOF
'Set the text boxes in the VAT Summary
strNet = "txt_N" & CStr(intRow)
Me!strNet = recVat![SumOfnet_t]
'Move to the Next Record
recVat.MoveNext
intRow = intRow + 1
Loop

Just don't use the bang notation, but the normal notation, and this should be rather easy:
Me.Controls(strNet).Value = recVat![SumOfnet_t]

Related

VBA ACCESS code autoincrementing a value with prefix

I have a table named 'odonto' and it has the fields code (autoincremental), history, surnames and names. I need to generate the code so that it autogenerates the HISTORY obtaining the first letter of the last name which will then have to be concatenated with consecutive numbers for each letter. That is to say that if we have four "FLORES" and a "MENDOZA" in the register it shows in a text box the next samples:
F001
F002
F003
F004
M001
...
Also I need to keep in mind that if a record is deleted it will be replaced by incrementing it again.
I did it and it functions for the asigning value, but it doesn't replace the deleted one if it.
Private Sub APELLIDO_AfterUpdate()
Dim MyStr
MyStr = Left([APELLIDO], 1)
Me.LETRA = MyStr
If IsNull(Me.HISTORIA) Then
Me!HISTORIA = ((MyStr) & "0000" & ([Cant] + 1))
Else
HISTORIA = Me.HISTORIA
End If
Me.Refresh
End Sub
Please your help.

vbscript code to delete a row in a csv file where cell is empty

I have a csv file that I need to have converted to an xlsx file (not a problem)...in the process of doing that I format my data. I want to delete all rows where I have an empty cell. Column C had text data and Column D has numeric data.
I have tried :
If (.Sheets(1).Columns.Cells("D")) = "" Then
msgbox("HI")
End If
I've tried .text as well as .value I can't seem to get into the actual cells to determine if it's empty so as to delete the row.
You need a little more structure to achieve your goal. You can't check every cell in Column D just by using .Cells("D") = "" as the Cells expects both row and column indicators to be provided.
Perhaps you could try looping over your data in Column C and/or D and where there's an empty cell, delete the whole row.
Const xlUp = -4162 ' Excel variables are not defined in vbscript
Dim oBook : Set oBook = ThisWorkbook
Dim oSheet : Set oSheet = oBook.Sheets(1)
Dim iLastRow, iRow
iLastRow = oSheet.Cells(oSheet.Rows.Count, 3).End(xlUp).Row
For iRow = iLastRow to 2 Step -1 'assumes a header row otherwise use 1 instead of 2
If oSheet.Range("C" & iRow) = "" Then
oSheet.Range("C" & iRow).EntireRow.Delete ' delete row if blank
End If
Next
Edit after comment:
Changed the loop to run from iLastRow to 2 instead of 2 to iLastRow. When deleting rows, you should always move from the bottom up, as when you delete a row, the next one jumps up and is then skipped over!

Access VBA- insert checkbox after text in Word Table

my VBA dynamically writes several tables depending on criteria and works quite well.
For example here is adding a row, formatting it and advancing to the next row.
oDoc.Tables(t).Rows.Add 'add a row below control number
oDoc.Tables(t).Rows(i).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
oDoc.Tables(t).Rows(i).Shading.BackgroundPatternColor = oTableHeaderColor
i = i + 1 'advance to row below control number row.
oDoc.Tables(t).Cell(i, 2).Range.Text = "Check Definition"
I do it in this manner because I just don't know how many rows any given table is - so I create the rows as I need them, might be clumsy but it works.
What I need to do is add a check-able checkbox to rows with text like this.
Planned: ☐
I've tried several ways that just don't seem to work. as far as I can tell it's because i'm not creating the table then selecting it. I've tried recording a macro and that just shows the bit about selection first.
Here's what I've got, it pops an error.
oDoc.Tables(t).Cell(i, 2).Range.Text = "Planned: " & oDoc.Tables(t).Cell(i, 1).Range.ContentControls.Add(wdContentControlCheckBox)
I get "requested member of the collection doesn't exist.
If I try to put it on two lines it will just overwrite cell 1. with the checkbox and I can't seem to position it to the end of the cell first. Any ideas? I've tried insertbefore and that works but I have to insert several checkboxes in the same cell.
Any ideas?
thank you.
Getting text + checkbox + other text + second checkbox into one cell is tricky.
This may be one of the few cases, where you actually have to use the Selection object.
This works for me in Word:
Dim oDoc As Word.Document
Set oDoc = ThisDocument
Const t = 1
Const i = 1
Dim rng As Word.Range
Dim iCheck As Long
Dim sLabel As String
Set rng = oDoc.Tables(t).Cell(i, 2).Range
rng.Select
With Selection
.Collapse Direction:=wdCollapseStart
For iCheck = 1 To 3
sLabel = Choose(iCheck, "Planned: ", " Done: ", " Perhaps: ")
.TypeText Text:=sLabel
.Range.ContentControls.Add wdContentControlCheckBox
' move cursor after checkbox
.MoveRight Unit:=wdCharacter, Count:=2
Next iCheck
End With
In Access, use oWord.Selection instead (or what you have as reference to Word).
The following works for a single checkbox, but I didn't manage to create a second one after additional text.
Dim rng As Word.Range
Set rng = oDoc.Tables(t).Cell(i, 2).Range
rng.Text = "Planned: "
' set range to end of cell
rng.Collapse Direction:=wdCollapseEnd
' I'm not entirely sure why this is needed, but without the checkbox goes into the next cell
rng.MoveEnd Unit:=wdCharacter, Count:=-1
rng.ContentControls.Add (wdContentControlCheckBox)

expression to check if barcode input exists in which case load data for that barcode else continue to create new record

Hi I'm using Access 2007 and i am doing a program to enter stock items.
I am using a form and i need to make sure that when the user inputs the barcode of the product the system checks in the table if this exists. If it does exists, i need to load data for this existing item into the 3 additional fields in the same form, otherwise to continue creating the new record.
Now i am trying to use set tempvar in the beforeupdate however i cannot get it right.
any suggestions please.
field name : [barcode]
table to look into is "cartridge static data"
additional fields to fill if barcode exists are : [cartridge] , [end user] , [phone no]
Appreciate any help
regards
Tony
I would insert a combo box using the Access wizzard.
Select the data from "cartridge static data" i.e. [barcode] [cartridge] [end user] [phone no]
Do not hide the first row, and make sure you can see the data width in the wizard as you build it.
When completed go to the combo [data] [row source] and click the three {…}
Check what is displayed ~ for example sort by Barcode, remove nulls etc.
If you have the column widths wrong you can change those in the [format tab].
Column headers default to No which may need changing or you may be happy with that.
Under [other] name check you have a sane name e.g. cbo_barcode_search
Now attach this code to the AfterUpdate property of the Combo Box:
Sub cbo_barcode_search_AfterUpdate ()
Dim rs As DAO.Recordset
If Not IsNull(Me.cbo_barcode_search) Then
'Save before move.
If Me.Dirty Then
Me.Dirty = False
End If
'Search in the clone set.
Set rs = Me.RecordsetClone
rs.FindFirst "[BarCode] = " & Me.cbo_barcode_search
'rs.FindFirst "[BarCode] = """ & Me.cbo_barcode_search & """" 'for text
If rs.NoMatch Then
'Trigger new form or add the just typed data into your form as required
‘e.g. me.field1 = cbo_barcode_search.column(0) ' Barcode
Else
'Display the found record in the form.
' Usually use Me.Bookmark = rs.Bookmark but your question suggests this is not what you want so
'NOTE: First column data is column(0) NOT column(1)
me.field1 = cbo_barcode_search.column(0) ' Barcode
me.field2 = cbo_barcode_search.column(1) ' Cartridge
me.field3 = cbo_barcode_search.column(2) ' end user
me.field4 = cbo_barcode_search.column(3) ' phone no
End If
Set rs = Nothing
End If
End Sub
You will need to modify this to match your field names (eg me.field1 is probably me.barcode but it may be me.str_barcode ~ I don't know what you used.
Hope this gets you on the right track. Paul

how to update text field using listbox

i have access 2007 and i want to update a text field [eng] using a list box [List191] which contains 3 values value1,value2,value3
i want when i click on this list and select one or two values i get this values as text separated with (,) in that text field
something like :
Private Sub List191_Click()
Form_tbltest.[eng].Value = Form_tbltest.[eng].Value &","& Form_tbltest.List191.value
End Sub
this code is not working with me , any suggestions ???
If you have a multi-select list box, look at the Access help topics for ListBox.ItemsSelected Property and ListBox.ItemData Property.
In this example, I chose the list box's After Update event for the code. I named my text box txtEng. The code loops through the list box's ItemsSelected collection, and adds the ItemData value for each to a string variable, strEng. After the loop, the leading comma is discarded when the length of that string is > 0. Finally the string's value is assigned to the text box.
Private Sub List191_AfterUpdate()
Dim strEng As String
Dim varItem As Variant
For Each varItem In Me.List191.ItemsSelected
strEng = strEng & "," & Me.List191.ItemData(varItem)
Next
If Len(strEng) > 0 Then
strEng = Mid(strEng, 2) ' discard leading comma
End If
Me.txtEng = strEng
End Sub