I'm new to vb, so sorry if my question has been asked before. I have searched but have not been able to find, or perhaps recognize, an answer. I am using visual studio 2010 and creating an app in vb.net.
I have 2 arrays named questions and answers.I want to traverse these 2 arrays simultaneously and retrieve the records from these 2 arrays.which i am doing with the following code.
Dim sql As String = "SELECT QuestionId,Answer FROM tbl_Answers"
Dim dt As DataTable = obj.GetDTbl(sql)
For Each row As DataRow In dt.Rows
SelectedAnswers += row("Answer").ToString & ","
Next
Dim Questions() As String = QuestionsIds.Split(",")
For Each question In Questions
Dim answers() As String = SelectedAnswers.Split(",")
For Each answer In answers
Dim rbl As RadioButtonList = DirectCast(plcHolderForm.FindControl("Question_" & question), RadioButtonList)
rbl.SelectedIndex = rbl.Items.IndexOf(rbl.Items.FindByValue("answer".ToString))
In above code it is obviously can be seen that my outer for loop executes only once.I need to traverse above 2 for each loop such that against each question i can get its answer which am retriving through FindByValue function
Can you just loop through both question and answer in 1 generic for loop?
Dim CurQuestion as String
Dim CurAnswer as String
For i As Integer = 0 To Questions.Length - 1
CurQuestion = Questions(i)
CurAnswer = Answers(i)
'Do what you need what current question and answer
Next
This assumes that you have the same number of questions and answers and they line up in their respective arrays. If not you'll have to fine tune it to meet your specific need.
Related
Sorry for the basic question, I've spent the best part of a hour figuring this out and I'm at my limit. I'm trying to add all values together if a string matches a certain size, for context I'm adding wall tiles together for a client, here's an example table;
Code
TileSize
TileQty
BG3215
8mm
1
BG3545
10mm
3
BG3246
8mm
4
BG3745
8mm
1
BG3255
12mm
2
My VBA:
Dim db As DAO.Database
Set db = CurrentDb
Dim eight As DAO.Recordset
Dim strSQLeight As String
strSQLeight= "SELECT SUM(TileQty) FROM PickQuery WHERE TileSize = '8mm'"
Set eight= db.OpenRecordset(strSQLeight)
'Error line ^
sometextbox = eight.Fields(0).Value
Run-Time error:
Too few parameters expected 3
Intended result is to return 6 (6 x "8mm" strings)
I assume there should be another argument in there but after looking in stack overflow and on google for help, I can't see another way to write this.
Your code works perfectly for me (running in an MSAccess DB, vba module):
Sub foo()
Dim db As DAO.Database
Set db = CurrentDb
Dim eight As DAO.Recordset
Dim strSQLeight As String
strSQLeight = "SELECT SUM(TileQty) FROM PickQuery WHERE TileSize = '8mm'"
Set eight = db.OpenRecordset(strSQLeight)
sometextbox = eight.Fields(0).Value
End Sub
Is there something we are missing? How are you running this code? Is it in an access vba module? Somewhere else?
Too few parameters expected 3
This means, that your query PickQuery is missing three parameter values.
Most likely, these are values fetched from the form where you call this code.
I have to dim n variables in my software, where n is a number that user types in the interface. I'd start with a for loop, in order to declare these variables, with something like:
for i = 0 to n
dim r.i as IRow = worksheet.CreateRow(i)
next
This is what I think but obviously I know that's wrong. Does anyone knows if this is possible? I can't create neither List(Of) nor arrays because these are rows of an excel file I'm trying to export, otherwise NPOI returns me error.
What does Excel have anything to do with not using a List? You can absolutely use worksheet.CreateRow() with a list:
Dim rows As New List(Of IRow)
For i As Integer = 0 to n - 1
rows.Add(worksheet.CreateRow(i))
Next
Now the name of each row is row(0), row(1), row(2) ... row(n - 1)
If you've tried this and NPOI is giving you an error, you should show that code around where the error is thrown and tell us what the error is.
Based on comments:
'Assuming everything has the same number of entries as ListBox1, per the comments
Dim rows As New List(Of IRow)
Dim row As IRow = worksheet.CreateRow(0)
row.CreateCell(0).SetCellValue("Time")
row.CreateCell(1).SetCellValue("hrr")
For i As Integer = 0 To ListBox1.Items.Count - 1
row = worksheet.CreateRow(i+1)
row.CreateCell(0).SetCellValue(ListBox1.Items(i))
row.CreateCell(1).SetCellValue(ListBox2.Items(i))
rows.Add(row)
Next i
I am working on a visual basic project. I have a mdb database connected to my project. I want to add a SELECT query that finds the results which are in array that i give it on my program
I have tried to write a statement like that:
SELECT kodu, adi_soyadi, sectigi_ders_say
FROM ogrenciler
WHERE kodu IN ?
But it does not work. In my page codes I have an array and I want to find results from "ogrenciler" table where the "kodu" is in my array.
Well, you could send that array to a temp table in Access, but that would prevent more then one user using the software at the same time. (or you could add some user name to the temp table. However, if the array of choices is small, say about max 50, then you can create the sql string.
eg:
Dim MySQL As String = "SELECT * from tblHotels WHERE ID IN("
Dim IdList(5) As Integer
Dim i As Integer
For i = 1 To 5
IdList(i) = i
Next
Dim MyList As String = ""
For i = 1 To 5
If MyList <> "" Then MyList = MyList & ","
MyList = MyList & IdList(i)
Next
MySQL = MySQL & MyList & ")"
Using MyCon2 As New OleDbConnection(My.Settings.OLESQL)
Dim da As New OleDbDataAdapter(MySQL, MyCon2)
Dim rstDat As New DataTable()
da.Fill(rstDat)
For i = 0 To rstDat.Rows.Count - 1
Debug.Print(rstDat.Rows(i).Item("HotelName"))
Next ' etc etc. etc.
End Using
So you can use the SQL format of:
SELECT * FROM tblHotels where ID IN (1,2,3)
And thus build up the "list". The only downside to this approach is that the sql string is limited to 2000 characters. So, if your list is larger then say 50 or so items, then you have to adopt a different approach.
In vb.net 2008, how can I display table column names in a combobox?
In SQL I have a Document table and there are columns in this table like Doc_id, Size, Path.
The combobox must display the columns name like
doc_id in the first line, size in the second line, path in the last line
Probably a little too late since I see this thing is about eight years old, but I just ran into this issue where I had to do exactly what the initial questioner was asking for. Since I resolved it, I will leave it here just incase another poor sap like us comes along looking for answers
'VB.net
Dim ds As DataSet = <define your dataset here>
Dim dt As DataTable = ds.Tables(<Your tablename here>)
For Each column As DataColumn In dt.Columns
combobox1.Items.Add(column.ColumnName)
Next
As per our discussion , i will suggest you not to bind the combobox directly with the DataTalble. you can bind it by looping all the row of the DataTable. you use following snipped as a reference:
Dim objDataTable As DataTable = ds.Tables("Document")
IF objDataTable <> NULL Then
For j As Integer = 0 To objDataTable.Rows.Count
Dim str As String = objDataTable.Rows(j)("Doc_id").ToString()
comboBox1.Items.Add(str)
str = objDataTable.Rows(j)("Size").ToString()
comboBox1.Items.Add(str)
str = objDataTable.Rows(j)("Path").ToString()
comboBox1.Items.Add(str)
Next
END IF
Hope this will work for you ;)
I have a text file that reads:
Left Behind,Lahaye,F,7,11.25
A Tale of Two Cities,Dickens,F,100,8.24
Hang a Thousand Trees with Ribbons,Rinaldi,F,30,16.79
Saffy's Angel,McKay,F,20,8.22
Each Little Bird that Sings,Wiles,F,10,7.70
Abiding in Christ,Murray,N,3,12.20
Bible Prophecy,Lahaye and Hindson,N,5,14.95
Captivating,Eldredge,N,12,16
Growing Deep in the Christian Life,Swindoll,N,11,19.95
Prayers that Heal the Heart,Virkler,N,4,12.00
Grow in Grace,Ferguson,N,3,11.95
The Good and Beautiful God,Smith,N,7,11.75
Victory Over the Darkness,Anderson,N,12,16
The last element of each line is a price. I would like to add up all the prices. I've been searching for so many hours now and cannot find a thing to answer my question. This seems soooo easy but I cannot figure it out!!! Please help out. BTW, this list is bound to change (adding of lines, deletion of lines, altering of lines) so if you can, please nothing concrete but instead leave the code open to changes. Thanks!!!
Just so you can see my pooooorrrr work, here is what I have (I think I deleted my code and rewrote a different way for several hours now.):
Dim Inv() As String = IO.File.ReadAllLines("Books.txt")
Dim t As Integer = Inv.Count - 1
Dim a As Integer = 0 to t
Dim sumtotal As String = sumtotal + Inv(4)
also,
for each line has either an "F" or an "N". how do I add up all the F's and all the N's. Do I do it via if statements?
First, you'll be better off using Double as your type instead of String. Second, observe how I use the Split function on each line, cast its last element as a double, and add it to the total. Yes, using an If Statement is how you can determine whether or not to add to the count of F or the count of N.
Dim lstAllLines As List(Of String) = IO.File.ReadAllLines("Books.txt").ToList()
Dim dblTotal As Double = 0.0
Dim intCountOfF As Integer = 0
Dim intCountOfN As Integer = 0
For Each strLine As String In lstAllLines
Dim lstCells As List(Of String) = strLine.Split(",").ToList()
dblTotal += CDbl(lstCells(3))
If lstCells(2) = "F" Then
intCountOfF += 1
Else
intCountOfN += 1
End If
Next