Open form to view related records if they exist or open blank form otherwise - vba

Hi I have a form called frmGeneralInformation where the main data is inputted about a book. Then I want a button that opens a form called frmFolio which shows the information about each page in the book. If records about the folios of the book already exist, the existing records should be shown with the option to add more folios. Otherwise a blank frmFolio should be created.
This is my VB code:
Private Sub FolioForm_Click()
Dim Found As Boolean
Found = Not IsNull(DLookup("SurveyID", "tblGenInfo_Folio", "SurveyID = " & .SurveyID))
If Found = True Then
Dim recordID As Integer
recordID = Me.FolioID
DoCmd.OpenForm "frmFolio", , , "FolioID = " & FolioID
Else
DoCmd.OpenForm "frmFolio"
End Sub
But I am getting this error:
Invalid or unqualified error
This is the relationship between the tables:

Related

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

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!

int array to returning to previous record

I have a standard form which displays information based on a partID. The form has a subform showing ancillaries of the part where on double click will take you to that part number (code snippet below). var_lastPartID is a global long variable which records the current ID so upon pressing a return button will take you back to the previous record. However, as this can only store 1 value at a time I imagine the best way would to be to store an array of long/int whereby upon the double click the current ID is stored. When you click return it will take the last value added and take you to that record then delete that record, so that the next time you click return it will take you to the next record. However, my experience with VBA is very limited and I have not used them before. Please could someone explain the syntax of how I could achieve this?
Private Sub childPart_DblClick(Cancel As Integer)
var_lastPartID = Forms![Part]![part_ID].Value
Forms("Part").Recordset.FindFirst ("part_ID = " & childPart)
End Sub
Ok, so you have a parent form, and then a sub form.
You click on a row in the sub form. You want the parent form to JUMP to this record. But as you may well do this several times, then you want on the main form some kind of “back” or “previous” button. When you click on this back button, then you want the previous ID that you jumped to occur.
Ok, the way to drive this and have a next/previous set of buttons that traverse the records that you looked at, worked on, and clicked on?
Ok, in the main form, at the code module level, create a pointer, and a collection, and a routine like this:
Option Compare Database
Option Explicit
Dim intListPosition As Integer
Dim colRecords As New Collection
Public Sub AddAndJump(MyID As Long)
' add this new ID to the list
intListPosition = intListPosition + 1
colRecords.Add MyID, CStr(intListPosition)
' now jump (move) to this record
Me.Recordset.FindFirst "ID = " & MyID
End Sub
Public Sub BackOne()
' call this code from your back buttion
' go to previous record
If intListPosition > 1 Then
intListPosition = intListPosition - 1
Me.Recordset.FindFirst "ID = " & colRecords.Item(CStr(intListPosition))
End If
End Sub
Public Sub ForwardOne()
'call this code from your forward buttion
If intListPosition < colRecords.Count Then
intListPosition = intListPosition + 1
Me.Recordset.FindFirst "ID = " & colRecords.Item(CStr(intListPosition))
End If
End Sub
Private Sub cmdBack_Click()
BackOne
End Sub
Private Sub cmdNext_Click()
ForwardOne
End Sub
So the above goes in the main form.
In my "sample" code, I used ID, you have to change that to part_id
Now, you button code in the sub form is going to be "similar" to what you have, but you can now use this:
Call Me.Parent.AddAndJump(Me.ID)
So, the above will now allow a next/back button on the top form, as you click away and select the items from the lower form (the sub form), then the list of "id" will be built up, and the buttons for next/prev will work.

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.

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.

Access 2013: Getting error message "Method 'Open' of object' _Recordset' failed " when trying to populate text boxes with data in a form

I'm having an issue with this piece of VBA code. I'm trying to populate text boxes on a form in Access 2013 with data from a table called Module. Once a user selects an available module from a combo box and clicks a button called 'btnSelectmodule', the relevant data should feed into the text boxes. Instead I'm getting an error; " Method 'Open' of object' _Recordset' failed "
Private Sub btnSelectmodule_Click()
Dim strmoduleid As String
Dim rstmodule As New ADODB.Recordset
strmoduleid = CVar(cmbSelectmodule)
rstmodule.Open "SELECT Module.ModuleID, Module.ModuleName,
Module.ModuleShortCode, Module.Level, Module.Semester, Module.CATPoints,
Module.FacultyCode, Module.Active, Module.Type FROM [Module]
WHERE (((CVar([ModuleID]))='" & CVar(strmoduleid) & "'));",
CurrentProject.Connection, adOpenKeyset, adLockOptimistic
If (rstmodule("ModuleID") <> "") Then
txtmoduleid.Value = rstmodule("ModuleID")
txtmodulename.Value = rstmodule("ModuleName")
txtmoduleshortcode.Value = rstmodule("ModuleShortCode")
cmblevel.Value = rstmodule("Level")
cmbsemester.Value = rstmodule("Semester")
cmbcatpoints.Value = rstmodule("CATPoints")
Active.Value = rstmodule("Active")
txttype.Value = rstmodule("Type")
End If
End Sub
This code works perfectly on three of my other forms. They populate data from different tables though. Could the problem have anything to do with the table being called Module? Because I've used [ ] in the SQL statement but still no joy? Any help would be appreciated. Thanks.
Inside the SQL string, you need to either :
a) change your table name to not be a keyword
b) surround all references in the SQL to Module with square brackets
c) use an alias: Select m.* FROM [Module] as m
d) change your table name to not be a keyword.