Ms Access AddItem with VBA - ms-access-2010

I have a Form that has a button on it. The button basically copies records from one Table to the other.
As records are read and copied, it is also checked for specific values. E.g. If one of the fields in the table has a value of "" then it should call another form that allows me to enter a date. Once the date is entered and the form is closed the programme carries on with copying.
It can also happen that the key fields in the table that is being copied are duplicate. In this case I should a 'listbox form' should be launched with a listbox displaying the values of the duplicate records. I should then select the correct record that I need copied.
Dim NumberCount As Long
NumberCount = RecordsetElementValue.RecordCount
If NumberCount > 1 Then
With Form_F_ListBox.List30
RecordsetElementValue.MoveFirst
Do
With Forms!F_ListBox.List30.AddItem(RecordsetElementValue!E_ElementValue)
End With
RecordsetElementValue.MoveNext
Loop Until RecordsetElementValue.EOF = True
DoCmd.OpenForm "F_ListBox", acNormal
End With
End If
The code sample above is what I have for in case there are duplicate records (NumberCount > 1)
The listbox in my F_ListBox form should be filled with the values in my recordset.
I now run into a runtime error 6014. The RowSourceType property must be set to 'Value List' to use this method.
What am I doing wrong?

The usual way to set the row source of a combo or list box in MS Access is to use an SQL statement, however, you can also use a list. This is controlled by the row source type.
Me.MylistBox.RowSourceType = "Value List"
From your notes, it seems that an SQL statement for the row source would be easier:
Me.MylistBox.RowSource = "SELECT ID FROM MyTable"

Related

Can't set ASP.NET drop down list to return query value in a datatable

Ok so I have been searching for three days and so far I can not find an answer that explains what I need to do.
Here is what I need. I have an asp.net content page. On that page there are some drop down list boxes. I have a query that fetches data from a SQL DB and puts in a Data table and that is working fine.
What I am trying to do is set the text of the ddl to a string that has been returned from the query.
Here is the HTML side:
<asp:DropDownList ID="ddlProbType" CssClass="ddlEdit" runat="server" Width="150px" DataSourceID="SdsProbType" DataTextField="probtype" DataValueField="probtype" />
Here is the VB code behind:
If dt.Rows.Count > 1 Then
stupidString = dt.Rows(RowCount)("probtype")
ddlProbType.SelectedValue = stupidString
Else
stupidString = dt.Rows(RowCount)("probtype")
ddlProbType.SelectedValue = stupidString
End If
I have added the variable stupidString and the set a break point to check the value being returned and it is correct. When the next line executes the ddl does NOT get the text assigned. I can mouse over the variable stupidString and see that the value it was assigned from the query is correct.
When I have tried setting the ddl directly from the data table, I have tried .ToSting() and .ToString.Trim() to the end of the dt.Rows line but it just will not assign!!
I know this probably something really stupid that I am not doing or overlooking but can anyone help? I do not want to assign a whole sql table to the ddl I just want to set the current list to the value that is returned by the sql query.
You need to populate the list first, e.g. by using
ddlProbType.Items.Add(New ListItem("Key","Value"))
or by using Data Binding.
After the list is populated, then you can tell the control which item should be selected.

Updatable Microsoft Access Combo box that doesn't insert into a table

I need a combo box that can be bound to a table and accept a new entry without inserting that new entry into the table until all of the other fields in the record is ready to be inserted. I tried using code where you use a SQL Insert statement however when I want to save the rest of the data on the form to a table the rest of the data is showing up on a new record. So I have one record with nothing but the project name and another record with everything else.
I have also tried this:
Append2Table = acDataErrContinue
vField = cbo.ControlSource
If Not (IsNull(vField) Or IsNull(NewData)) Then
sMsg = "Do you wish to add the entry " & NewData & " for " & cbo.Name & "?"
If MsgBox(sMsg, vbOKCancel + vbQuestion, "Add new value?") = vbOK Then
Set rst = CurrentDb.OpenRecordset(cbo.RowSource)
rst.AddNew
rst(vField) = NewData
rst.Update
rst.Close
Append2Table = acDataErrAdded
End If
End If
I don't want to use edit because I will have a lot of records with the same project name and when I used the insert statement to add the project name I don't know anyway to have it return the ID field so that I could search for that and edit the record with the rest of the information.
Thank you in advance for any suggestions.
What you want to do is to add the lookup item only if it is going to be used, hence the waiting for the insert.
I don't see how this is possible, because you are going to need the ID of the lookup value in order to do the insert.
I don't recommend doing the insert by hand, it is messy, let Access do it for you.
I think the best idea in this case is to let the user enter the lookup item and have some cleanup mechanism when the form closes to remove any unused items from the lookup table.

How to assign an ID but then delete if not used

I am unsure on how to do this 'best practice' wise.
I have a web application (asp.net VB) that connects to an MS SQL server 2012. Currently when the page loads the app connects to a DB table and gets the last ID and adds 1 to it and displays this to the user. When the user submits the form the new ID is saved to the DB.
The problem being the app may be opened by 2 users at the same time and therefore they will get assigned the same ref number which will cause problems when the data is saved.
How can I assign different numbers to different users if the app is opened at the same time without saving unnecessary data?
You have multiple solutions for this, I'll try to outline a few approaches. (I'll assume that you need to insert things into a DB that I'll call "orders".)
First of all, you can move the ID-generation to the moment when the order is actually inserted, and not at the moment when the user start to enter the data. That way, you do not generate an ID for a user that never completes the form. Also this scenario is easy to accomplish using autoincrementing values in sql server. You can, for example do:
-- create a table with an identity column
create table Orders (
ID int identity(1,1) not null,
Description nvarchar(max) not null
);
-- insert values, without specifying the ID column
insert into Orders (Description) values ()
-- select the row back
-- returns 1, 'My First Order'
select * from Orders;
Another way to do this is to use SQL Server Sequences. These are things that do nothing except generate numbers in a row. They guarantee that the numbers won't be repeated, and always keep count of the current value, i.e.
-- create a sequence
create sequence OrderIdSequence
start with 1
increment by 1;
-- get the next sequence value
select next value for OrderIdSequence

How can I store and retrieve data from a checkboxlist?

SQL Tables
Listing
ID, Title.....
ListingType
ID, Name
ListingMatrix
ListingID, ListingTypeID
Basically a listing can be more than 1 type and I want that to be able to be shown using the ListingMatrix table. However, I'm having a lot of issues populating the checkboxlist because I have it being sorted by Title to keep it user friendly. I'm using VB.Net, LINQ and MS SQL.
Dim readListingMatrix = (From ListingCategories In db.ListingTypeMatrixes _
Where ListingCategories.ListingID = ListingID)
For Each row In readListingMatrix
CheckBoxListListingCategories.Items(row.ListingTypeID - 1).Selected = True
Next
My issue is storing the checklistbox and editing it. Storing I think I could hack, but editing it is becoming a pain since I can't get the checkboxlist to check the correct boxes since their location changes due to the ORDER BY in my SQL Statement that populates the list.
Assuming that the value field of your checkboxes is filled with a ListingTypeID, do this:
Dim readListingMatrix = (From ListingCategories In db.ListingTypeMatrixes _
Where ListingCategories.ListingID = ListingID)
For Each row In readListingMatrix
CheckBoxListListingCategories.Items.FindByValue(row.ListingTypeID).Selected = True
Next

Equivalent of RecordSet.MoveNext while Not EOF in ASP.NET

I'm using a DataReader to display informations stored in a table.
I created Two button to go to next record and to go back.
In VB6 I used this code :
While Not Recordset1.EOF
Recordset1.MoveNext
End While
In ASP.NET I didn't find a way to do like it, because DataReader hasn't the EOF property.
EDIT :
While Not Recordset1.BOF
Recordset1.MovePrevious
End While
How can I convert this last code (VB6) to ASP.NET ??
You use Read instead of MoveNext and it'll return false if there aren't any more records.
So:
While rdr.Read()
.... ' process this row
End While
Azirar, ho1 is correct in that you should use a DataTable. If you're updating after every post back and only need a single record you could still use a DataReader, but set up your SQL statement to get a single row (storing the appropriate information needed in your SQL statement (or better yet stored procedure) to get that single record back within query strings or session variables).

Resources