Sql table with columns that can store arraylist [closed] - sql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a parameters that contain 6 or more number and can be input with textbox
so for each parameter, put the numbers in a arraylist
like this:
Dim a As New ArrayList
a.Add(Val(B1_Left.Text))
a.Add(Val(B1_Down.Text))
a.Add(Val(B1_Right.Text))
a.Add(Val(BB1_Left.Text))
a.Add(Val(BB1_Down.Text))
a.Add(Val(BB11_Right.Text))
so for each record i have an arraylist parameter that should be insert the row of one column. Which column type should i apply in sql server table to store a vb.net arraylist?
my vb.net code for insert to database:
Dim DBConnection As New SqlConnection(My.Settings.CS)
Dim DBCommand As New SqlCommand
Dim _PID As Integer
Dim _Date As Date
Dim _txt As String
Dim _B1 As ArrayList = a.clone
DBCommand.Connection = DBConnection
DBCommand.CommandText = "insert into TOM_ShaftFix " &
" ( PID, [Date], txt, B1) " &
" VALUES(#PID,#Date,#txt,#B1)"
DBCommand.Parameters.AddWithValue("#pid", _PID)
DBCommand.Parameters.AddWithValue("#Date", _Date)
DBCommand.Parameters.AddWithValue("#txt", _txt)
DBCommand.Parameters.AddWithValue("#B1", _B1)
DBConnection.Open()
DBCommand.ExecuteNonQuery()
DBConnection.Close()

Depending on the version of SQL Server you could store the data as JSON.
In SQL 2016 it is supported natively, alternativelly you could store it as XML.
declare #country nvarchar(max) = '{
"id" : 101,
"name": "United States",
"continent": "North America"
}';
INSERT INTO Countries
SELECT * FROM OPENJSON(#country)
WITH (id int,
name nvarchar(100),
continent nvarchar(100))
MSDN
Depending on the type you could simply store it as comma seperated values.
1,2,3,4,5,6

Related

how to fill down when empty cells in access db [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
db access table
kindly i need help to fill down with previous string PS: i am a beginner
Your Date field seems to be of data type Text. If so, change that to DateTime.
Then you can run a loop to update the records having no date value:
Dim Records As DAO.Recordset
Dim Sql As String
Dim LastDate As Date
Sql = "Select * From YourTable Order By [Surevey ID]"
Set Records = CurrentDb.OpenRecordset(Sql)
If Records.RecordCount > 0 Then
LastDate = Date ' Adjust if needed.
Records.MoveFirst
While Not Records.EOF
If IsNull(Records!Date.Value) Then
Records.Edit
Records!Date.Value = LastDate
Records.Update
Else
LastDate = Records!Date.Value
End If
Records.MoveNext
Wend
End If
Records.Close

How can i split text and insert into SQL Server (VB.NET) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a textbox like this
"AAA#BBB#CCC
DDD#EEE#FFF"
I want to use Vb.net split text and insert into SQL table like this:
Insert into table A (MAIN, REASON, RESULT) VALUES (AAA, BBB, CCC)
Insert into table A (MAIN, REASON, RESULT) VALUES (DDD, EEE, FFF)
Please help me
Luckily .net provides String.Split() method. You must use parameters to avoid sql injection. The data is coming from the user.
Private Sub OPCode()
TextBox1.Text = "AAA#BBB#CCC DDD#EEE#FFF"
Dim split1 = TextBox1.Text.Split() 'no parameters split by space
'results in an array of 2 elements
Dim DataForInsert1 = split1(0).Split("#"c) 'small c indicates to compiler that this is a Char
Dim DataForInsert2 = split1(1).Split("#"c)
Using cn As New SqlConnection("Your connection String"),
cmd As New SqlCommand("Insert Into TableA (MAIN, REASON, RESULT) Values (#Main, #Reason, #Result);")
cmd.Parameters.Add("#Main", SqlDbType.VarChar, 100).Value = DataForInsert1(0)
cmd.Parameters.Add("#Reason", SqlDbType.VarChar, 400).Value = DataForInsert1(1)
cmd.Parameters.Add("#Result", SqlDbType.VarChar, 400).Value = DataForInsert1(2)
cn.Open()
cmd.ExecuteNonQuery()
cmd.Parameters("#Main").Value = DataForInsert2(0)
cmd.Parameters("#Reason").Value = DataForInsert2(1)
cmd.Parameters("#Result").Value = DataForInsert2(2)
cmd.ExecuteNonQuery()
End Using

How to pass variables from VBA through SQL? [duplicate]

This question already has answers here:
How do I use parameters in VBA in the different contexts in Microsoft Access?
(2 answers)
Closed 4 years ago.
I'm trying to pass values from a Form into my database, via SQL:
Private Sub Befehl4_Click()
Dim SQL As String
Dim unique As String
unique = Forms!frm_test!PN
SQL = "INSERT INTO tab_test (Feld1) VALUES (unique)"
DoCmd.RunSQL SQL
End Sub
When I execute this code from my button in my Form, unique isn't passed to the SQL-command. I have to type it in. When writing the SQL-command this way:
SQL = "INSERT INTO tab_test (Feld1) VALUES (Forms!frm_test!PN)"
It works how expected.
What am I missing or doing wrong?
SQL = "INSERT INTO tab_test (Feld1) VALUES ('" & Forms!frm_test!PN & "')"

Ms Access SQL: Concatenate seperated by commas one to many relationship [duplicate]

This question already has an answer here:
Combine values from related rows into a single concatenated string value
(1 answer)
Closed 6 years ago.
With the following kind of table:
tblRequest
-RequestID(double)
-RequestDescription (String)
tblError
-ErrorID(long integer)
-ErrorName(String)
-RequestID(double)
The above relationship is a ONE to MANY relationship.
I want to VIEW the data in the following manner. Therefore, I need a SELECT query which displays the data in the following manner.
Request Error(s)
1 Error1, Error6
2 Error2, Error3
3.4 Error4, Error2, Error1
I tried to search for an answer which involved FOR XML PATH('')). However, I do not think it can work in Ms-Access.
Here's a potential solution.
Step 1:
Create this function in your MS Access App.I don't think this is the most efficient solution, however it should work well enough if the number of records isn't very large.
Public Function getErrorText(ByVal MyId As Double) As String
Dim myrs As Recordset
'Create the recordset
Set myrs = CurrentDb.OpenRecordset("select distinct ErrorID from tblError where RequestID = " & MyId)
'Build the error string
Do Until myrs.EOF
getErrorText = myrs.Fields(0).Value & ", " & getErrorText
myrs.MoveNext
Loop
'Clean up
myrs.Close
Set myrs = Nothing
'Return the correct cleaned up string
getErrorText = Left(getErrorText, Len(getErrorText) - 2)
End Function
Step 2:
You should then be able to run the following SQL statement to get the output desired.
SELECT distinct tblError.RequestID, getErrorText( tblError.[RequestID]) AS [Error(s)]
FROM tblError INNER JOIN tblRequest ON tblError.RequestID = tblRequest.RequestID
WHERE (((getErrorText( tblError.[RequestID])) Is Not Null));

MS Access - Select rows according to a field whose values are in an array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am using MS Access 2007.
I would like to extract data from a table in a database according to some field, say 'car ID'. For every 'car ID' (total of 100 cars), I want to extract all the rows related to that car into an excel spreadsheet in order to operate on it later. Ideally, I would have 100 different spreadsheets in the end, each describing the history of a single car.
Now, since I have a vector of 100 car IDs, I would like to select the car ID automatically as if it was within a for loop, rather than running a query manually 100 times.
I think I should use VBA for that, but since I am new to both MS Access and VBA I am not quite sure of how to handle this.
Any hint would be greatly appreciated.
Thank you very much.
So you need 2 tables. Your main table contains the CarID as the key field and the second table contains all our values/interests. It seems you are set up that way already.
For this example below, you will need to create a select query and save it with the name "mySavedQuery" (It doesn't matter what the select query does at this point because we will change it through the code.)
In the on-click of a command button you could do:
Dim rs As DAO.Recordset
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String
Dim intLow As Integer
Dim intHigh As Integer
Dim i As Integer
Set db = CurrentDb
intLow = 1
intHigh = DMax("CarID", "Table2")
For i = intLow To intHigh
strSQL = "SELECT Table1.CarID, Table2.Interest FROM Table1 INNER JOIN Table2 ON Table1.CarID = Table2.CarID " _
& " WHERE (((Table1.CarID)=" & i & "));"
Set rs = CurrentDb.OpenRecordset(strSQL)
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
Set qdf = db.QueryDefs("mySavedQuery")
qdf.SQL = strSQL
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "mySavedQuery", "C:\Users\SClark\Desktop\Survey" & i & ".xls", True
End If
Next i
Set rs = Nothing
Set db = Nothing
Set qdf = Nothing
What is happening here is we determine the minimum CarID = 1 and use the DMAX to find the Max CarID. We then iterate these values and export one Excel sheet per CarID.
This is a down and dirty example but should get you started down the right path...