Add a row using linq or sql in VB.NET - sql

I am developing an application, and as one of the steps, I need to have a set of information backed up to an SQL database.
I don't have any experience using SQL, and was hoping I could find a simple command that would just add a row with the information i'd needed... But I don't understand much of the information I've found for it. I've even had issues just using the snippet provided within Visual Studio.
I tried using the snippet for adding a row, and here's the code that's produced.
The reference to RDataSet.ArchivedIncidentsDataTable keeps giving me an error message telling me "object reference to non-shared object requires an object reference", which to me, is one of the least helpful error messages you could possibly receive. I don't understand what it means, or what I need to do in order to fix it.
Here is the code that I have, which is basically just the snippet that's provided:
Private Sub Save_SQLBackup_Info()
Dim newRow = CType(RDataSet.ArchivedIncidentsDataTable.NewRow(), RDataSet.ArchivedIncidentsRow)
newRow.CustomerID = "A124"
newRow.CompanyName = "Acme"
RDataSet.ArchivedIncidentsDataTable.Rows.Add(newRow)
End Sub
I'm not sure how filling in the fields for this small amount of code produced an error.
Also, after this segment of code is run, don't I have to run a command to update the database? The guides I'd been following online all referenced something like that, but I didn't understand the other parts of them.
I'm sure the problem i'm having is a matter of "You didn't declare X.", however I don't know much about SQL... If that's even what I'm using.
I would appreciate some guidance.
All I need to do is add a row to the database using a few fields from the form.
I have an online SQL server linked as a data object, and the dataset is titled RDataSet , the data table is titled ArchivedIncidentsDataTable.
The few guides I've found online reference different parts, like data adapters, that I don't have, and / or don't reference how they got to that part... I'm completely lost.
Is there any sort of one-line command I can run that will just shoot the information I'd like added to a row into this database? I don't understand why it has to be this complicated...
EDIT:
One of the answers was to drag an instance of the dataset onto the form.
After doing this, and trying to reference it, I'm now getting an error message telling me :
"Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.
This message appears under the autocorrect options, and gives me the option to replace the dataset I added to the form, RDataSet1, with RDataset.
When hovering over the code "RDataSet1.ArchivedIncidentsDataTable.NewRow()", I still see the error message "Object Reference requires an object reference".

The message
object reference to non-shared object requires an object reference
means you're trying to access a member of a class like it's a shared member, but it's not, so you need an actually instance of that class (an object reference).
If you're using WinForms, you can just drag a RDataSet onto your Form, and it should generate a field in your Form called RDataSet1 or something like that (I asumme you used the DataSet Designer of Visual Studio).
Then use RDataSet1 (the object reference) instead of RDataSet (the type).

Related

BC: How to add a dataset and modify a report from 'base microsoft' with a 100xx ID?

I am trying to modify a report that is given by base microsoft and I can't figure out how to 'replace' and add a dataset the 10081 report.
I have tried copying the report and modifying it, but I need to add a dataset and don't have access to the al code due to it being a base microsoft report.
I tried to make a new report, it just wont appear when I try and got o the returns. When I try to replace the 10081 report with the custom 5014x report it gives me an odd error like 'textbox billtity needs to be named billtocity' even though it is. I have also included the '=First(... , 'dataset') in the expresssion
Any youtube links, links to documentation or anything would be helpful.

MS Access: Query to list number of files in a series of folders

I have folders labeled by their keyfield, so 1, 2, ... 999, 1000. located in currentproject.path\RecordFiles\KeyFieldHere so like currentproject.path\RecordFiles\917.
I want to run a query that will count how many files are in each folder. I know this can be done with the DIR function through visual basic, but I can't seem to run it through a SQL query.
I've tried using this function in a SQL equation, so Expr1: [FlrFileCount("Y:\Education\Databases\RecordFiles\")] as one of the fields just to see if it can work, but it prompts me for a value and then returns nothing.
EDIT: I tried an approach using the FlrFileCount function in a continuous form, and it does work, BUT... I get an error after every single line. I have a field in a continuous form of =FlrFileCount([currentproject].[path] & "\recordfiles\" & [ID]), but when I run the form I get an error "Error 76, Error source: FlrFileCount, Error description: Path not found." Which is crazy because IT WORKS, it properly lists the number of files in the folder for each record.
I just need to get this functionality over into a SQL query so I can pull that data for mail merges.
I currently have something similar in a form. The form has an onload property to run a module (Link here) to create a list of all the files in the relevant folder to that record, and then I have another field that just counts the number of entries in the list. However a list can't be a value in a SQL query, so I don't think that code will help.
Thanks to Tim Williams, the answer was to put
=FlrFileCount(Currentproject.Path & "\recordfiles\" & [ID])
It seems the [currentproject].[path] part was where the error was. What's confusing is that in other places, MS Access adds the extra [] around currentproject and path, and I don't know why.
Thank you so much for your help! Now to the tricky part: Implementing a proper naming scheme by program ID across a sharepoint so that the relevant folder can be opened consistently even when program names change.

How to create a program to convert unit measurements

Using Microsoft access, visual basic.
I'm having a big problem doing this task.
What I have done: Created a table on access where I have put measurements in (from meters):
mile = 10000meters, nautic mile = 1862meters, English mile=1652, kilometers = 1000 meters and all the way down to Millimeters.
What I have created for input:
1 box takes an Integer to be converted and a 1 box specified with an initial unit.
What I have created for Output:
1 box shows the Integer of result with 1 box specified the chosen unit of the output.
Can anyone please, please help me with the codes?
Honestly I'd never really noticed the CONVERT function until today but here's a quick demo of how I'd slap together a "conversion tool" in Excel.
If you want to do the same thing in Access, the premise is the same, but it will be a bit more work since you'll have to design the form from scratch instead of using a worksheet, which is kind of meant for this kind of job.
Using Excel functions in Access
Before you are able to use Excel's CONVERT function in Access, you'll need to reference the Microsoft Excel Object Library.
In Access, open any VBA Module.
GoTools > References
Check the box next to Microsoft Excel 16.0 Object Library. (The version number will vary if you have an older version of Office.)
Then you can call most Excel functions from Access VBA or queries with WorksheetFunction (the same way you would use them in Excel VBA).
For example:
MsgBox WorksheetFunction.Convert(3.7, "m", "ft")
...displays a message box with the number of feet in 3.7 metres.
The calculations will be the easy part; a couple lines of VBA in the On Change or On Exit events will trigger the calculation.
The most time-consuming part will likely be perfecting the placement and formatting of the controls on the form, which is by no means difficult (and there are several tutorials online that can provide the basics if necessary.)
Lastly, keep in mind that there are no doubt a plethora of existing conversion tools available for free download with a little Googling... (I'm confident that you're not the first person who wanted to use MS Office to convert measurements.) 😉
More Information:
Microsoft Docs : WorksheetFunction.Convert Method
Microsoft Docs : List of Worksheet Functions Available to Visual Basic
Office Support : Create a form in Access
QuackIt: Microsoft Access Tutorial
Blueclaw : Access Event Procedures
You can download the demo xlsx used above from JumpShare here.
For both comboboxes, bind them to column 2, faktorTilMeter, and set the ColumnWidths to, say: 2,542cm;0cm.
Then, assign this expression as ControlSource for your output textbox:
=TextboxInput/ComboboxFrom*ComboboxTo

VBA Extra key/item pairs in scripting.dictionary

This code was working for several days, looping through a dictionary just fine. The loop started breaking, and I have extra keys in the dictionary and I have no idea where they came from.
I am creating a scripting.dictionary with the following:
Dim riskDict As New Scripting.Dictionary
After which, the object is empty, as seen in the watch:
I then add my first key/item pair:
riskDict.Add "Weight", Array("WP", 0)
And after running this one line of code, I now have this:
Where did these two extra keys (Item 2 and 3) come from?! This is a problem, since later in my code I use:
For Each key In riskDict
temp = riskDict(key)
...
Next key
And this loop breaks, since it starts referring to keys which are empty. This does not seem to have been happening until just now, and the code has been running fine for a few days. (I changed some things elsewhere in the code, but completely unrelated to this.)
Let me know if I am doing anything ridiculous, or missing something obvious, and thanks!
Make riskDict private or clear your array before using it. Hard to tell what your doing with it since there is critical elements missing.
Dim riskDict As New Scripting.Dictionary
Although this is an old question, it might still be of interest to other users.
Before the first item has been added to an initialized dictionary object, the local window of the VBA editor must not be switched on if a breakpoint exists in the scope of this Dictionary object. Otherwise a key-value pair (0, Empty) might be inserted into the dictionary. This dictionary entry cannot be deleted again in the immediate window, even Dictionary.RemoveAll fails. This is the case even if the local window has been closed again in the meantime.
The exact circumstances under which this behavior occurs are not clear to me: I've only observed this behavior of Dictionary objects in class modules so far, but on the same line of code it sometimes occurs, sometimes not.
This SO discussion also describes the behavior when a watch is set on the Dictionary object. There, this behavior is associated with the property of dictionaries that when a query is made for an unknown key, this key is automatically added with a value of 'Empty'.

SSRS custom code and variables life

I have a report that needs to process the data that it get from SQL before show it.
For that, I have a custom code, and a Dictionary where I push all the processed data.
My problem is that if I save the dictionary in a report variable when I export the report to Word that variable seems to be cleaned.
What is the lifecycle of the reports variables? What is the most convenient way of saving an object during the report life.
Thanks!
I have been playing around with custom code for a about 6 weeks so I can answer some parts of the question of variable lifecycle in SSRS 2008 R2.
I have report that uses a Dictionary to store totals, allows me provide some specialist subtotals for financial stuff. I have something you can check (as I can't yet comment on things).
Have you declared the variable as 'shared', this is a custom code specific keyword that doesn't translate into VB.net. It ensures the variable lives to the next page, I tested this to Excel and word seemed to work fine transferring over the variable's data.
There is a trade off however under SSRS "report on demand" engine (on web, but not on BIDS) it holds the variable and doesn't garbage collect until the cache itself is cleared. I wrote some more custom code to indicate when my parameters changed and clears the variable.
Code;
Public Shared Dim Totals As New System.Collections.Generic.Dictionary(Of String, Decimal)
Public Function WipeKeys() as Decimal 'Clear Data from Dictionary (this will clear the cached object as well)
Totals.Clear()
Return 0D
End Function
I will also to recommend overwrite the key where ever possible to ensure reduction of addition loops.
Regards,