"NotOnList" event not recognizing that new data has, in fact, been added - vba

This used to work but now it doesn't. Did Microsoft change something?
I'm attempting to understand the behavior of a combo box in MS Access. I created a table named "Colors" with 2 fields: ID (Autonumber, primary key) and Color (short text). I made a form with a combobox with "LimitToList" set to Yes. The Row Source is the table "Colors". For the NotInList event I have the following code:
Private Sub Colors_NotInList(NewData As String, Response As Integer)
Dim rst As DAO.Recordset
If MsgBox(NewData & " is not in list. Add it?", vbOKCancel) = vbOK Then
Response = acDataErrAdded
Set rst = CurrentDb.OpenRecordset("Colors")
With rst
.AddNew
!Color = NewData
.Update
End With
Else
Response = acDataErrContinue
ctl.Undo
End If
End Sub
If I enter a color not in the table I get the prompt to add it to the list. On clicking OK I get the Access error that I must select an item on the list. The new color I just entered does appear on the list and if I select it everything works fine. But I didn't used to have this second step of selecting it after it's been entered.
I've spent way to much time on what should be a simple task. Can anyone help? Thanks!

Related

Append Strings and values together to target a form control in VBA

I'm so close to getting this code working, I just need a little push please. I would like to
take the name of a combo box and then add a string to the end, But then get the value of a textbox with that string. This is to create a dynamic function instead of pasting the same code over and over.
Here's what I have so far, after you select something in the dropdown, the data is then pulled to populate the boxes next to it. I have about 8 drop downs so far so that's why I need this to work.
'Combobox after update
Call GrabData(Me, Me.ActiveControl)
Then
Private Sub GrabData(ctl As Control)
'name of ctl/combobox is "Kitchen"
data1 = (ctl.Name & "Size") '"KitchenSize"
'Here is where it all goes wrong
data1.Value = size.value
'size.value is just a textbox for example
End Sub
I can debug this with:
msgbox(data1)
'outputs "KitchenSize"
But I cannot get the value of kitchensize's textbox with data1.value
Error:
Object Required
I have also added Dim As String / Dim As Control.
I will be assigning the variable to some other stuff in this 50 line code I wrote so please don't take the above example as exactly what I intend to do, I just need help appending the ctl.name to a string, then use that to reference another control and so on.
EDIT
For anyone who wants to know, I figured it out.
Dim Ctrl As Control
Dim CtrlName As String
CtrlName = ctl.Name & "Size"
Set Ctrl = Me.Controls(CtrlName)
Ctrl.Value = 'Wherever you want to send the values to
See the edit.
You need to dim it as a string, then use Set Ctrl

Check if data exist in file

I need help. I want to check if user exists by entering their ic number and I want to display another rest of their data by using file in visual basic. Unfortunately, an error occurs while doing that. I need help. If the user exists, then It will display automatically name, email, address and so on but if a user doesn't exist, then it shows message box. Here I attached the image of the display screen and the code. Please help me. Thank you.
Public Class Form1
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim userFile As String = "C:\Users\HP\Desktop\userdata.txt"
Dim inputFile As String
If System.IO.File.Exists(userFile) = True Then
Dim objReader As New System.IO.StreamReader(userFile)
Dim intIc As Integer
Dim intCount As Integer = 0
Dim strName As String
Dim strEmail As String
Dim intPhoneNum As String
Dim strAdd1 As String
Dim strAdd2 As String
Dim intPostcode As String
Dim strState As String
Do While objReader.Peek() <> -1
intIc(intCount) = Convert.ToInt64(objReader.ReadLine())
If (intIc(intCount).Convert.ToInt64(objReader.ReadLine())) Then
strName(intCount) = objReader.ReadLine()
strEmail(intCount) = objReader.ReadLine()
intPhoneNum(intCount) = Convert.ToInt32(objReader.ReadLine())
strAdd1(intCount) = objReader.ReadLine()
strAdd2(intCount) = objReader.ReadLine()
intPostcode(intCount) = Convert.ToInt32(objReader.ReadLine())
strState(intCount) = objReader.ReadLine()
lblName.Text = strName
lblEmail.Text = strEmail
lblNum.Text = intPhoneNum
lblAdd1.Text = strAdd1
lblAdd2.Text = strAdd2
lblPostcode.Text = intPostcode
lblState.Text = strState
objReader.Close()
Else
MessageBox.Show("User Does Not Exist")
End If
intCount = intCount + 1
Loop
Else
MessageBox.Show("File Does Not Exist")
End If
End Sub
End Class
Your task, the easy way:
make a new project
add a DataSet to this new project
open the DataSet, in the properties call it something sensible
Right click the surface, add a new datatable, name it Person
Right click the datatable, add a column, name it IC. Right click, add column, name it Name. Keep going until you added all the fields you want to track(email,phone,address1 etc)
save the DataSet
open the form
show the datasources window (view menu.. other windows)
expand the nodes til you can see Person
click the drop down next to Person, switch from DataGridview to Details
drag Person onto the form. Text boxes, labels etc appear. In the tray at the bottom more things appear
add a textbox to the form and call it searchTextBox
add a search button to the form, double click it, add this line of code to the click handler:
personBindingSource.Filter = '[ic] LIKE '" & searchTextBox.Text & "'"
If personBindingSource.Count = 0 Then MessageBox.Show("No records")
double click the form background to add a form load event handler, put this line of code:
If IO.File.Exists("data.xml") Then .ReadXml("data.xml")
switch back to designer, single click the form background and switch to event properties of the form, add a handler to the form closing event:
.WriteXml("data.xml")
That's it, you now have a program that will open, read and fill the DataSet with data from the data.xml file, it will search it when you type something in the ic box, the text boxes use databinding to show values automatically, and when you close the program it will save updates data. The only task now is to load the xml file with data.
When the textboxes were added to the form you should also have seen a bar appear across the top with some left/right controls in and a green plus. Click the green plus, type some data in, click it again, type more data. Navigating back, if you're adding new data, will commit the data. If you're looking at existing data, editing it then navigating will commit it
After you added some data, you can search for existing data using the search box. When you've searched for a single value it should be the only thing shown and the nav will show "1 of 1". To get back to the mode where all data is showing, put a single asterisk in the search box and hit search; it should show the number records in the top bar and you can scroll them with the arrows.
If you already have lots of data in a file, like you use in your question, you can read it in a loop (like you do in your question, except don't use that code exactly cos it has loads of errors) as a one time thing and assign it into the datatable, or you can manipulate it directly into being XML in a text editor. This is easy to do if you have a capable text editor but I'll not offer any particular advice on it in case you don't have a large amount of existing data. Ask a new question if you do

VBA ListView control - retrieve columns from selected item

I have a ListView object within my Microsoft Access database that is set to Report View. When a user selects a row from this list, I want to be able to open a secondary form and populate based on the selected values. I am running into trouble with the proper syntax for capturing the column values I need. I have searched high and low looking for information on this, however I have not been able to find anything that fits my particular criteria.
Here is the code in question:
Private Sub ListView1_ItemClick(ByVal Item As Object)
Dim FormID, FilingID, RowIndex As Integer
Dim FilingName As String
RowIndex = Item.Index
FormID = Item.SubItems(0)
FilingID = Item.SubItems(1)
FilingName = Item.SubItems(2)
If MsgBox("Do you want to open up filing " & FilingName & "?", vbYesNo, "Confirmation") = vbYes Then
DoCmd.OpenForm "frmFiling", acNormal, , , , , "FormID=" & FormID & ";FilingID=" & FilingID
End If
End Sub
This code keeps throwing an exception on the line FormID = Item.SubItems(0) stating that it is an invalid property value.
Does anybody know what I am doing wrong here, or have a workaround that accomplishes what I am trying to do?
In your line FormID = Item.SubItems(0) where you declared FormID as a variant, you are assigning an object to it, so VBA expects SET FormID = Item.SubItems(0).
However what you want is probably FormID = CInt(Item.SubItems(0).Text)
I changed the event from ItemClick to Click, and used ListView1.SelectedItem.ListSubItems(1).Text to extract the column value.
Thanks everyone for your comments.

Pop up window with textbox after selecting checkbox

all
I am working on a project with vb.net and MySQL database.
Now for taking information I have added few checkboxes, and in case if user selects a checkbox named other I want a window to appear and it should have a text box and when user enters the text at that box, the details should be stored in db.
As you haven't provided much information I may can't answer exactly your question but check if this can help.
First Add LINQ to SQL (.dbml File) - Let's name it as XYZ and Dataset (.xsd File) - let's name this as XYZ too, to your project and then drag and drop your database table in both the files and save all.
Now going into your form which contains the check box you mentioned.
Add this code to you checkbox click_event.
If checkbox1.checked = True Then
Dim insertValue As String = ""
insertValue = InputBox("Enter text to insert", YourTitle, "")
If inserValue <> "" Then
Dim db as New XYZDataContext
Dim NewRec As New YourTableName With {.ColumnName = insertValue}
db.YourTableName.InsertOnSubmit(NewRec)
db.SubmitChanges()
Msgbox("Value added!")
End If
checkbox1.checked = False
End If
create your window in the designer and give it a name like someWindow. then in your code open the window from the click event of your checkbox. when you close the window don't dispose it. just hide it me.hide so when you window closes you can retrieve the data from your textbox.
Sub Test ()
Dim wd_SomeInfo as new someWindow
wd_SomeInfo.showdialog()
Dim result As String = wd_SomeInfo.txt_sometextbox.text
If result = "" Then cancel....
End Sub

Can not update List Box Selected Collection in code

I have a list box on a form being populated from a query, with items selected on basis of matching a delimited list. So sSystemString equals something like "A;B;C"
Then I load records A,B,C,D,E,F from the SQL Server DB and only A,B,C should be selected.
Is there a native way to do this in MS Access (2010). I'm using an ADP in this case.
I'm doing it via code but I can't the selected property does not reflect my changes, nor does the form.
Here's my code:
Dim rs As New ADODB.Recordset
Dim sSystemString As String
If Not IsNull(Me.OpenArgs) Then sSystemString = Me.OpenArgs
' Load this list box with SRC Systems
Call rs.Open("SELECT DISTINCT System FROM dbo.System WHERE System IS NOT NULL", _
CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly)
Do Until rs.EOF
lstSrcSystems.AddItem (rs.Fields(0))
If InStr(sSystemString, rs.Fields(0)) > 0 Then
lstSrcSystems.Selected(lstSrcSystems.ListCount - 1) = True
End If
rs.MoveNext
Loop
My code definitely hits the lstSrcSystems.Selected(lstSrcSystems.ListCount - 1) = True line.
After running this line, inspecting the property in the immediate window still returns 0 (it doesn't change). On the form, the item is also not selected.
UPDATE: I just checked my code again and now it is being updated, but the next AddItem apparently unselects it again.
I suspect I have some weird combination of properties that make this read only, but I can select items interactively, and indeed when I extract the selected items back off in code, the Selected property works as expected - i.e. I select an item on the form and it reflected in this property.
The form is unbound and is called from a button on another form with this code:
DoCmd.OpenForm "fSiteList", acNormal, , , acFormEdit, acDialog, Me.SRCSystems
The problem was actually that lstSrcSystems.AddItem (rs.Fields(0)) was resetting the Selected state. I can't find any mention of this behaviour or how to turn it off. I altered my form as follows:
Form / Data properties all blank (unbound)
List Control / Data / Control Source: blank
List Control / Data / Row Source: SELECT statement populating my list
This has the effect of populating the list but not binding the list or form to anything. (I found that binding it stopped me interactively editing data on it)
Form_Load code behind the form was changed to select already existing items:
Private Sub Form_Load()
Dim sSystemString As String
Dim iIndex As Integer
' List is bound to site list (from connections)
' Highlight those that are listed
If Not IsNull(Me.OpenArgs) Then sSystemString = Me.OpenArgs
iIndex = lstSrcSystems.ListCount
Do While iIndex > 0
If InStr(sSystemString, lstSrcSystems.ItemData(iIndex)) > 0 Then
lstSrcSystems.Selected(iIndex) = True
End If
iIndex = iIndex - 1
Loop
End Sub
I'm still curious to know whether there is a more 'built in' way to achieve this: edit a delimited string field.