Trouble with using multiple actions within a if statement - windows

everytime i code using multiple actions, it doesnt produce and output and the code doesnt work at all until i give it one action alone instead of three, i dont know whats wrong with it, i tried to put this code in if statements and/or just left it alone as an action when pressing the save button
heres the code \ btw im using visual studio 2012
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim Path1 As String = "Backups\"
Dim Path2 As String = rtbTitle.Text + "\"
Dim FullPath As String = Path1 + Path2
Dim textfileTitle As String = "title.txt"
Dim textfileDescription As String = "description.txt"
Dim textfileTag As String = "tag.txt"
Dim textfileChannel As String = "channel.txt"
If Directory.Exists(FullPath) Then
rtbTitle.SaveFile(FullPath + textfileDescription, RichTextBoxStreamType.PlainText)
rtbDescription.SaveFile(FullPath + textfileDescription, RichTextBoxStreamType.PlainText)
rtbTag.SaveFile(FullPath + textfileDescription, RichTextBoxStreamType.PlainText)
End If
End Sub

Add Option Explicit and Option Strict to the top of your code file (or set them as defaults) and you'll soon see where you're getting into trouble.
One problem that will show up is that you're using '+' to concatenate strings, you really should use '&'. You may unwittingly have typos in your control names or be inputting bad data for the paths. All these sorts of errors are covered up until run time unless you set those options.
After that add a Try...Catch block around your code, run it and view the error.

Related

Expected Function or variable vb6.0

Hello i am coding in Visual Basic 6.0 and i have this error, i am trying to make a button inserting data in database.
Expected Function or variable
This is my code
Private Sub Command1_Click()
With ConString.Recordset.AddNew
!ID = txtID
!Emri = txtEmri
!Mbiemri = txtMbiemri
!Datelindja = txtData
!Telefon = !txtTelefon
!Gjinia = gender
!Punesuar = job
!Martese = cmbMartese
!Vendlindja = txtVendlindje
End With
End Sub
txtID is textbox
txtEmri is textbox
txtMbiemri is textbox
txtData is date picker
txtTelefon is textbox
gender is a string that takes value if a radio button is clicked
job is an integer if a checkbox is clicked
cmbMartese is combo box
txtVendlindje is textbox
Thank you in advance.
#JohnEason gave you the right answer. But there's another error in the line !Telefon = !txtTelefon, if I'm not mistaken. It should read !Telefon = txtTelefon, i.e. without the exclamation mark in front of txtTelefon.
I'm also not a big fan of that coding style. I prefer to not rely on default properties, but instead "spell out" the whole term, e.g.
With ConString.Recordset
.AddNew
!ID = txtID.Text
!Emri = txtEmri.Text
!Mbiemri = txtMbiemri.Text
!Datelindja = txtData.Text
!Telefon = txtTelefon.Text
' Since VB6 doesn't provide Intellisense for variables, I prefer to use some
' kind of hungarian notation for variable names, in this case sGender or strGender for a string variable
!Gjinia = gender
' Same here: iJob or intJob for an integer variable
!Punesuar = job
' You need to specify that you want the selected item of a combobox
!Martese = cmbMartese.List(cmbMartese.ListIndex)
!Vendlindja = txtVendlindje.Text
' If you're done setting column values and don't do so somewhere else,
' you also need to add a .Update statement here in order to finish the
' .AddNew action and actually persist the data to the database.
' .Update
End With
End Sub
As pointed out below by #JohnEason, there's also potentially an .Update statement missing within the With/End With block

How do I add a save feature to a datagridview?

Hi i am fairly new to visual studio and I am having some trouble adding a save feature to my program. Basically what my program does is set event reminder for the user (it's like a daily planner but with no notifications). I have got the "add event', "delete", and "update buttons to work on the program and now all I have left is the "save" and "load" key. What I am trying to do is find a way to save the DataGridView so it can be opened back up in the program at a later date using the "load" key. If it would be easier to just remove the "load" and save the info right into the event reminder application, I could go that route but I don't have the first idea how to do that. This is what I have right now for the code in the main form:
Public Class MainForm1
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If DataGridView1.Columns(e.ColumnIndex).Name = "Delete" AndAlso Me.DataGridView1.Rows(e.RowIndex).IsNewRow = False Then
Me.DataGridView1.EndEdit()
Me.DataGridView1.Rows.RemoveAt(e.RowIndex)
End If
If DataGridView1.Columns(e.ColumnIndex).Name = "Column4" AndAlso Me.DataGridView1.Rows(e.RowIndex).IsNewRow = False Then
Dim Update As UpdateWindow
Update = UpdateWindow
Update.Show()
End If
End Sub
Private Sub dltBtn_Click(sender As Object, e As EventArgs)
Dim dltBtn As dltWindow
dltBtn = dltWindow
dltBtn.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Button1 As addBtn
Button1 = addBtn
Button1.Show()
End Sub
Private Sub UptBtn_Click(sender As Object, e As EventArgs)
Dim UptBtn As UpdateWindow
UptBtn = UpdateWindow
UptBtn.Show()
End Sub
Dim thisFilename As String = Application.StartupPath & "\Event reminder.dat"
Private Sub saveBtn_Click(sender As Object, e As EventArgs) Handles saveBtn.Click
Me.Validate()
Me.SaveGridData(DataGridView1, thisFilename)
End Sub
Private Sub BtnLoad_Click(sender As Object, e As EventArgs) Handles BtnLoad.Click
Me.LoadGridData(DataGridView1, thisFilename)
End Sub
Private Sub SaveGridData(ByRef ThisGrid As DataGridView, ByVal thisFilename As String)
ThisGrid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText
ThisGrid.SelectAll()
IO.File.WriteAllText(thisFilename, ThisGrid.GetClipboardContent().GetText.TrimEnd)
ThisGrid.ClearSelection()
End Sub
Private Sub LoadGridData(ByRef ThisGrid As DataGridView, ByVal Filename As String)
ThisGrid.Rows.Clear()
For Each THisLine In My.Computer.FileSystem.ReadAllText(Filename).Split(Environment.NewLine)
ThisGrid.Rows.Add(Split(THisLine, " "))
Next
End Sub
End Class
I have gone on the other forums and asked about how to do this and they all said the bind the datagridview to a data table. If that is the route I have to go, how do I go about it? If anyone has some examples or code I could try out I would be much appreciative.
The problem I can see in the posted code is between the LoadGridData and the SaveGridData methods. When the LoadGridData method runs, it reads a file where each line is “Split” on spaces “ “. This is fine and if the file is properly “space” delimited it works as expected. So let us assume everything works and the data displays in the grid properly.
Then the user presses the saveBtn which calls the SaveGridData method. The code appears to “select” all the cells in the DataGridView to the clipboard, then proceeds to write the clipboard text to the SAME file name used when loading the grid. This also appears to work as expected.
The problem with this is that when the code copies all the cells of the DataGridView to the clipboard then, writes this copied clipboard text; it will use a “Tab” delimiter. The file Event reminder.dat which was read in using a “space” delimiter will be overwritten with a “tab” delimiter and not a “space” delimiter. Therefore, then next time the load method is run… it will fail.
If the load and save button are going to read the same file, then they MUST both agree on what character is used as a delimiter. You can use whatever character you want as a delimiter. You can use a “space” as the posted code does, however this limits each type to a string with no spaces. If one of the fields is a home address, chances are good, there will be a space in the string. Using a “space” as a delimiter is not necessarily the best of choices.
A simple solution is available with the current code. Currently if the file Events reminder.dat is space delimited and reads in properly to the data grid view, then it should be easy to replace the space delimiter with something else. In this case, since the SaveGridData method is delimiting the fields with a “tab” then we simply need to change the load method to “split” on “Tabs” and not “spaces” when reading back the file.
Step 1 – make a copy of Events reminder.dat
Step 2 – Run the program, press the load button and make sure all the data is properly loaded into the grid.
Step 3 – If the data is displayed properly in the grid, press the save button. (Updates file with tab delimiters)
Step 4 – Exit the program and make the change below in the LoadGridData method.
ThisGrid.Rows.Add(Split(THisLine, vbTab))
Step 5 – Run the program. Now both the load and write methods agree on what character (Tab) is used as a delimiter.
Lastly, to comment on using a DataTable the answer would be yes. DatagridViews and DataTables play nicely together. Hope this helps.
You may want to take a “Tour” of Stackoverflow to see how it works.

Conversion.VAL method - Global Hook

Our application is very large and we have been using VAL method a lot to ensure that data input in the textboxes is converted properly into related numbers. However, now we ran into few issues. For example, if someone writes 25,500, the VAL function will return 25 instead of 25500. Seems like it is expected behavior as VAL method stops proceeding once it find nonconvertible character like comma.
I would like to know if there is a way to have a global hook for this method I can create in my application, so that whenever I call a VAL method from my application, it should call my defined method, instead of calling from the Microsoft.Visualbasic namespace.
thank you
With a textbox a button and a label on the form this seems to do the trick.
Make a convert sub and call it.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim result, middle As String
Dim newresult As Double
If TextBox1.Text Is Nothing Then
MsgBox("Empty TextBox!")
End If
result = (TextBox1.Text)
middle = result.Replace(",", String.Empty)
newresult = Val(middle)
Label1.Text = newresult.ToString
TextBox1.Text = Nothing
End Sub
Hopefully this helps. newresult is preserved as a double for further use, and the value is displayed only for clarity in this example

how to send contents of a textbox in vb6 to a notepad document

I am trying to send the data inserted into a textbox into a notepad document so that the data can be held for future reference, as i am trying to create an assessment date reminder as i am a student.
But whenever i try to send the data with this code by writing data = "txtAssign_Name, txtAssign_Due" when i check the notepad after running the program, all that is in the document is txtAssign_Name, txtAssign_Due.
Private Sub Form_Load()
Dim Data As String
txtAssign_Name.Text = ""
txtAssign_Due.Text = ""
Data = ""
Open "F:\Personal\date calender test - Copy\test.txt" For Output As #1
Print #1, Data
Close #1
End Sub
Try this:
Data = txtAssign_Name.Text & ", " & txtAssign_Due.Text
(In the lines above, you will want to NOT set those values to "")
Note - using the built in VB6 writing to text files will result in your output being formated in a way so that VB6 can read it back in again. So all strings will have quotes aroud them, other values are displayed in a variety of odd ways, commas seperate all values.
If the output text file is desired to be primarily for human use, then I find using the richtextbox control a better way of creating the file. Add it from Project -> Components -> Controls -> Microsoft Rich Textbox Control 6.0
Add the Rich text box to your form (you can set visible = false if you don't want users to see it). Add your data to it something like this:
RichTextBox1.text = txtAssingn_Name.text & ", " & txt_Assing_Due.text
' If you need to add more things, add them on to the end like this
RichTextBox1.text = richtextbox1.text & vbcrlf & "More text on the next line"
Then, when all the data is added, you just do this
RichTextBox1.SaveFile "C:\Test.txt", rtfText
rtfText is a visual basic constant, that tells the RTB to save as plain text. The alterntaive is rtfRTF. You can learn more about the RTB on MSDN
OK, so a comment above states that you want to be able to read the data back into VB.
The code as it shown in the question will just output a blank text file, as the variable Data is set to "", so that is all that will be output.
What you need is something like this
private Sub SaveData()
dim SaveFile as integer
SaveFile = Freefile ' This gets a file number not currently in use
Open "F:\Personal\date calender test - Copy\test.txt" For Output As #Savefile
write #SaveFile, txtAssign_Name.Text, txtAssign_Due.Text
close #savefile
end sub
And call SaveData in the appropriate place. NOT form load, as the user won't have entered anything at this point. Either after the user has pressed a button, or on Form_Unload if you want it to autosave on form close.
To populate your form again, something like this
private sub LoadData()
dim LoadFile as integer
dim StringData as string
loadfile = freefile
Open "F:\Personal\date calender test - Copy\test.txt" For Input As #loadfile
input #loadfile, StringData
txtAssign_Name.Text = StringData
input #loadfile, StringData
txtAssign_Name.Text = stringdata
close #loadfile
end sub
You should also add error handing whenever working with files. VB6 is quite particular and its very easy to throw an exception when dealing with files.

ArcPad - VBscript - Autopopulate attributes

I am using the following script to grab parcel and address information from one layer to fill the attribute table of a newly created feature.
There is no returned error, but the problem I am having is that there seems to be the wrong information stuck in the memory of recordselect function. No matter where I place a point it gives the same parcel # and address. Or maybe it isn’t actually be performing the IF function properly.
Sub Address
Dim rsCurrentXY
Set rsCurrentXY = Map.Layers("Violations").records
rsCurrentXY.movelast
Dim objXYShape
Set objXYShape = rsCurrentXY.Fields.Shape
Dim pControls
Set pControls= Application.Map.selectionlayer.Forms("EDITFORM").Pages(“PAGE1”).Controls
Dim rsGrid
' Find corresponding map page to the valve point
Set rsGrid = Map.Layers("ACPA_parcels").records
rsGrid.movefirst
Do While Not rsGrid.eof
If rsGrid.fields.shape.Ispointin(objXYShape) Then
pControls("txtAddress").value = rsGrid.Fields("ADD1").Value
Exit Do
End If
rsGrid.Movenext
Loop
' Clean Up
Set rsCurrentXY = Nothing
Set objXYShape = Nothing
Set rsGrid = Nothing
End Sub
(I have another subroutine called "PIN" that would do the exact same thing.)
I have them called when their respective edit boxes in the custom form are activated by the inspector.
Thanks for the help,
Robert
Accessing the EDITFORM via Application.Map.selectionlayer.Forms("EDITFORM") will be problematic. Whenever working with controls on an EDITFORM you should using ThisEvent.Object to discover all your objects. For example, if your event handler is Page_OnLoad then ThisEvent.Object will refer to your current page. You should have code like this:
Dim pPage1
Set pPage1 = ThisEvent.Object
Dim pControls
Set pControls = pPage1.Controls

Resources