How to get Volume Serial Number using Visual Basic 2010? - visual-studio-2010

I'm trying to get Volume Serial Number using Visual Basic 2010,
Is there a whole code example that shows me how to do this?
Thanks

I guess the simplest answer to my question was given by:
Hans Passant:
From his link,
I just copied and pasted this function and it works for Microsoft Visual basic 2010 express, Without any modifications
Public Function GetDriveSerialNumber() As String
Dim DriveSerial As Long
Dim fso As Object, Drv As Object
'Create a FileSystemObject object
fso = CreateObject("Scripting.FileSystemObject")
Drv = fso.GetDrive(fso.GetDriveName(AppDomain.CurrentDomain.BaseDirectory))
With Drv
If .IsReady Then
DriveSerial = .SerialNumber
Else '"Drive Not Ready!"
DriveSerial = -1
End If
End With
'Clean up
Drv = Nothing
fso = Nothing
GetDriveSerialNumber = Hex(DriveSerial)
End Function
I would like to thank everyone for their help,
And i apologize for repeating the question,
I did do a google search and a stackflow search,
But my search was"
"get hard drive serial number in visual basic 2010"
So this website did not show up,
Thanks again

This thread here http://social.msdn.microsoft.com/Forums/vstudio/en-US/43281cfa-51c8-4c35-bc31-929c67abd943/getting-drive-volume-serial-number-in-vb-2010 has the following bit of code that you could use/adapt.
I made a piece of code for you to show all drive information.
The Volume serial number is included you can get that by simple
putting some more if's in the code
Imports System.Management
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim drivetype() As String = {"Unknown", "NoRootDirectory", _
"RemoveableDisk", "LocalDisk", "NetworkDrive", "CompactDisk", "RamDisk"}
Dim allDrives() As String = Environment.GetLogicalDrives()
For Each drive In allDrives
Dim win32Drive As String = _
"Win32_LogicalDisk='" & drive.Substring(0, 2) & "'"
Dim Disk As System.Management.ManagementObject _
= New System.Management.ManagementObject(win32Drive)
Me.ListBox1.Items.Add(drive.ToString & drivetype(CInt((Disk("DriveType").ToString))))
For Each diskProperty In Disk.Properties
If Not diskProperty.Value Is Nothing Then
Me.ListBox1.Items.Add("---" & diskProperty.Name & "=" & diskProperty.Value.ToString)
End If
Next
Next
End Sub
End Class

Related

edit through visual studio an msword file

i am trying a program that can edit an existing ms word document through an instant editing at a visual studio 2010 program but im having trouble. my codes:
Imports Microsoft.Office.Interop
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim wd1 As Word.Application
Dim wd1Doc As Word.Document
wd1 = New Word.Application
wd1.Visible = True
wd1Doc = wd1.Documents.Add("C:\Users\DELL\Desktop\activity6\profile.dotx")
With wd1Doc
.FormFields("w_name").Range = TextBox1.Text
.FormFields("w_age").Range = TextBox2.Text
End With
wd1 = Nothing
wd1Doc = Nothing
End Sub
End Class
the errors says: "property range is read only"
Your code is almost correct. If you want to the the text of a Range object, you need to use its .Text property:
With wd1Doc
.FormFields("w_name").Range.Text = TextBox1.Text
.FormFields("w_age").Range.Text = TextBox2.Text
End With

Winsock Error 429: activeX component can't create object

So I know that my code below works. The purpose is to create a tcp Ethernet connection between a scale and computer, so that when a weight is read on the scale, the value is displayed on the computer at the push of a button. I copied this code to a new lab machine that was just imaged for me. As for the winsock, I dynamically created it at run-time by adding it to the references. I understand that this is not what I am supposed to do (see: https://support.microsoft.com/en-us/kb/313984).
With a breakpoint at the CFixPicture_Initialize function, the code hits "set tcpC = new Winsock" line and breaks with error 429: avtiveX componenet can't create object. Does anybody have any ideas as to how I can get this license/get this Winsock control to work? Thanks!
Option Explicit
Private WithEvents tcpC As Winsock
Private Sub CFixPicture_Close()
Set tcpC = Nothing
End Sub
Private Sub CFixPicture_Initialize()
Set tcpC = New Winsock
tcpC.LocalPort = 0
tcpC.Connect "192.168.0.1", 8000
End Sub
Private Sub CommandButton1_click()
On Error GoTo errHandler
tcpC.SendData "S" & vbCrLf
Exit Sub
errHandler:
MsgBox "error:" & Err.Description
End Sub
Private Sub tcpC_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Dim strDataString As String
tcpC.GetData strData
strDataTrim = Mid(strData, 11)
Text1.Caption = "Weight: " & vbCrLf
The control is not present or is present but not registered on the new machine.
Copy over mswinsck.ocx from your *system directory to the new machines *system directory
Open a console as admnistrator and run regsvr32.exe c:\whatever\mswinsck.ocx
*\System32 or \SysWoW64 on 64 bit Windows.
As there was no license for the Winsock, I found a license online. I simply ran this program and voila! The Winsock worked. Thanks!
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=4860&lngWId=1

csv file import to access database using vba

Im using visual basic and visual studio 2010.
I researched on importing csv files to access database and I found this generic codes. The problem is, I'm really new in visual basic. I declared the variables but I got the error: "Declaration expected".. and the Do while and Loop are having an error which is: "Statement cannot appear outside of a method
Public Class Form1
Dim strPathFile As String
Dim strFile As String
Dim strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean
blnHasFieldNames = True
strPath = "C:\Users\vissia18\Desktop\ReportDB\"
strTable = "Report"
strFile = Dir(strPath & "*.csv")
Do While Len(strFile) > 0
strPathFile = strPath & strFile
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
strTable, strPathFile, blnHasFieldNames
strFile = Dir()
Loop
End Class
The first thing I see is that no method or subroutine has been declared.
That is essential.
How about declaring a Main like this:
Public Class Form1
Sub Main()
Dim strPathFile As String
Dim strFile As String
...
End Sub
End Class
This will give your application a staring point.
May I suggest MSDN - Microsoft Developer Network - specifically the video walk through labelled "Visual Basic Fundamentals: Development for Absolute Beginners".

Error when creating MS Excel docs with VB 2010

I'm having some trouble with looping and creating MS Excel docs, code snippet below
Private Sub selectedRowsButton_Click( _
ByVal sender As Object, ByVal e As System.EventArgs) _
Handles selectedRowsButton.Click
Dim selectedRowCount As Integer = _
DataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected)
If selectedRowCount > 0 Then
Dim sb As New System.Text.StringBuilder()
Dim objexcel As New Excel.Application
Dim i As Integer
Dim FACode As Integer
Dim Sitename As Integer
Dim Sitecode As Integer
Dim Address As Integer
Dim City As Integer
Dim State As Integer
Dim ZIP As Integer
FACode = 1
Sitename = 5
Sitecode = 2
Address = 6
City = 7
State = 9
ZIP = 10
Dim xlWorkbook As Excel.Workbook
xlWorkbook = objexcel.Workbooks.Open("template path")
For i = 0 To selectedRowCount - 1
objexcel.Visible = True
objexcel.Range("B2").Value = DataGridView1.SelectedCells(Sitename).Value.ToString()
objexcel.Range("B3").Value = DataGridView1.SelectedCells(Sitecode).Value.ToString()
objexcel.Range("B5").Value = DataGridView1.SelectedCells(FACode).Value.ToString()
Dim thisfile As Object
thisfile = objexcel.Range("B5").Value & "." & _
objexcel.Range("B3").Value & "." & "otherstring" & "." & "otherstring2" & "." & ".xls"
With objexcel
xlWorkbook.SaveAs(Filename:="c:\test\" & thisfile)
'~~> Close the Excel file without saving
xlWorkbook.Close(False)
End With
Next i
End If
I'm getting the error Exception from HRESULT: 0x800A03EC for the statement
objexcel.Range("B2").Value = DataGridView1.SelectedCells(Sitename).Value.ToString()
IF I select only one row of my DataGrid before creating the program works fine, it is when I select multiple rows that this error occurs. Since I'm creating the program specifically for multiple row selections I'm stumped as to where I've gone wrong. Any help or pointers appreciated, Thanks!
Two things
You have declared objexcel As Excel.Application so you shouldn't use objexcel.Range("B2").Value. Use xlWorkbook.Range("B2").Value. Change it everywhere in your code.
You cannot use SaveAs like that. See the snapshot below. If you want to save as xls file then you have to use FileFormat:=56
See this code example
'~~> Save As file
xlWorkbook.SaveAs(Filename:="c:\test\" & thisfile, FileFormat:=56)
If you do not specify the file format then you will get an error message when you open the file after opening.
You might want to look at this link on how to automate Excel from VB.Net
Topic: VB.NET and Excel
Link: http://www.siddharthrout.com/vb-dot-net-and-excel/
I am not too sure what you exactly are trying to do with the DGV. Like Sean mentioned you are not incrementing the values. If you can post a snapshot of how your DGV looks and how your Excel file should look after the export then we can help you in a much better way :)

Visual Studio. How to copy record from database to word .doc and print it

In Visual studio 2010>New Project>Visual Basic>Windows>Windows forms Application, i have made a form (form1.vb) and a database (Local Database>"Database1.sdf") and a Table with 3 Columns ("Name","City","Age").
I like to copy this 3 fields and paste to document "test1.doc" (open this with Ms Office or Open Office Writer). I have bookmarks ("PasteName", PasteCity", "PasteAge") in specified places in test1.doc .
How to make a button to open the document "test1.doc" and copy - paste this 3 items from table to doc and preview before print it? (not for save - only print preview and close without save after printing)
I have find this code for MS Office but didn't work in Visual Studio. I like something similar. (this code is for a doc Form Fields - I have Bookmarks in my doc).
Private Sub cmdPrint_Click()
Dim appWord As Word.Application
Dim doc As Word.Document
Set appWord = GetObject(, "Word.Application")
Set appWord = New Word.Application
Set doc = appWord.Documents.Open("C:\WordForms\CustomerSlip.doc", , True)
With doc
.FormFields("fldCustomerID").Result = Me!CustomerID
.FormFields("fldCompanyName").Result = Me!CompanyName
.FormFields("fldContactName").Result = Me!ContactName
.Visible = True
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
End Sub
Thanks programers people
This works for me. (button action)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Print customer slip for current customer.
Dim appWord As Word.Application
Dim doc As Word.Document
'Avoid error 429, when Word isn't open.
On Error Resume Next
Err.Clear()
'Set appWord object variable to running instance of Word.
appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'If Word isn't open, create a new instance of Word.
appWord = New Word.Application
End If
doc = appWord.Documents.Open("D:\Test.docx", , True)
doc.Visible()
doc.Activate()
With doc.Bookmarks
.Item("Name").Range.Text = Me.NameID.Text
.Item("City").Range.Text = Me.CityID.Text
End With
Dim dlg As Word.Dialog
dlg = appWord.Dialogs.Item(Word.WdWordDialog.wdDialogFilePrint)
dlg.Display()
'doc.Printout
doc = Nothing
appWord = Nothing
Exit Sub
errHandler:
MsgBox(Err.Number & ": " & Err.Description)
End Sub

Resources