VBA Brush Up 03:Variables Data Types And Constant

2024-01-09 15:32

本文主要是介绍VBA Brush Up 03:Variables Data Types And Constant,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

来自:Julitta Korol,“Access.2003.Programming.by.Example.with.VBA.XML.and.ASP”,by Wordware Publishing, Inc. 2005, p28-p51, p69-p71

Technorati 标签: constants, variables, data types, vba, type conversion

 

tt

  1. The variable type can be indicated by the As keyword or by attaching a type symbol. If you don’t add the type symbol or the As command, VBA will default the variable to the Variant data type.

    1. Dim FirstName$ '相当于Dim FirstName as string 
    2. Dim age% 
    3. FullName$ = "John Smith" 
    4. Debug.Print FullName$ & " is " & age% & " years old." 


    tt

  2. Visual Basic automatically initializes a new variable to its defau lt value when it is created. Numerical variables are set to zero (0), Boolean variables are initialized to False, string variables are set to the empty string (" "), and Date variables are set to December 30, 1899.

  3. VBA has special functions that allow you to change the format of data.
    Format(expression, format)
    where expression is a value or variable you want to format and format is the type of format you want to apply.

  4. The Option Explicit statement will cause Visual Basic to generate an error message when you try to run a procedure that contains undeclared variables. One big advantage of using Option Explicit is that misspellings of variable names will be detected at compile time. The Option Explicit statement must appear in a module before any procedures. To automatically include Option Explicit in every new module you create, follow these steps:
    1. Choose Tools | Options.
    2. Make sure the Require Variable Declaration check box is selected in the Options window (Editor tab).
    3. Choose OK to close the Options window.

  5. When you declare a local variable with the Static statement, the value of the variable is preserved after the procedure in which the variable was declared has finished running. Static variables are reset when you quit the Microsoft Access application or when a run-time error occurs while the procedure is running.

  6. Module-level variables are declared at the top of the module (above the first procedure definition) by using the Dim or Private statement(Dim和Private等价). These variables are available to all of the procedures in the module in which they were declared, but are not available to procedures in other modules.
    If you declare a public variable in a module that contains the Option Private tatement, this variable will be available only to procedures in the current database. The Option Private statement can be used at the module level to indicate that the entire module is private.

  7. Type Conversion:

    1. Sub AddTwoNums() 
    2.     Dim myPrompt As String 
    3.     Dim value1 As String 
    4.     Dim mySum As Single 
    5.     Const myTitle = "Enter data" 
    6.     myPrompt = "Enter a number:" 
    7.     value1 = InputBox(myPrompt, myTitle, 0) 
    8.     mySum = value1 + 2 
    9.     MsgBox mySum & " (" & value1 & " + 2)" 
    10. End Sub

     

    because the value1 variable’s data type is String, prior to using this variable’s data in the computation, Visual Basic goes to work behind the scenes to perform the data type conversion. Visual Basic has the brains to understand the need for conversion. Without it, the two incompatible data types (text and number) would generate a Type Mismatch error.

    • To avoid the Type Mismatch error, use the built-in CSng function to convert the string stored in the value1 variable to a Single type number. You would write the following statement:
      mysum = CSng(value1) + 2
  8. The variables you’ve learned so far are used to store data. Object variables don’t store data. They store the location of the data. After declaring an object variable, you also have to assign a specific value to the object variable before you can use this variable in your procedure. You assign a value to the object variable by using the Set keyword.If you omit the word Set, Visual Basic will display the error message “Run-time error 91: Object variable or With block variable not set.”

  9. you can quickly locate the definition of the variable by selecting the variable name and pressing Shift+F2. Alternately, you can choose View | Definition. Visual Basic will jump to the variable declaration line. To return your mouse pointer to its previous position, press Ctrl+Shift+F2 or choose View | Last Position.

  10. Visual Basic has a built-in VarType function that returns an integer indicating the variable’s type.
    ?varType(age)

    tt

  11. A constant, like a variable, has a scope.
    Const Age As Integer = 25, City As String = "Denver", PayCheck As Currency = 350

  12. Exploring Intrinsic Constants
    1. In the Visual Basic Editor window, choose View | Object Browser.
    2. In the Project/Library list box, click the drop-down arrow and select the Access library.
    3. Enter constants as the search text in the Search Text box and either press Enter or click the Search button.Notice that the names of all the constants begin with the prefix “ac.”
    4. To look up VBA constants, choose VBA in the Project/Library list box.Notice that the names of the VBA built-in constants begin with the prefix “vb.”

这篇关于VBA Brush Up 03:Variables Data Types And Constant的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/587597

相关文章

cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个?

跨平台系列 cross-plateform 跨平台应用程序-01-概览 cross-plateform 跨平台应用程序-02-有哪些主流技术栈? cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个? cross-plateform 跨平台应用程序-04-React Native 介绍 cross-plateform 跨平台应用程序-05-Flutte

论文翻译:arxiv-2024 Benchmark Data Contamination of Large Language Models: A Survey

Benchmark Data Contamination of Large Language Models: A Survey https://arxiv.org/abs/2406.04244 大规模语言模型的基准数据污染:一项综述 文章目录 大规模语言模型的基准数据污染:一项综述摘要1 引言 摘要 大规模语言模型(LLMs),如GPT-4、Claude-3和Gemini的快

FreeRTOS内部机制学习03(事件组内部机制)

文章目录 事件组使用的场景事件组的核心以及Set事件API做的事情事件组的特殊之处事件组为什么不关闭中断xEventGroupSetBitsFromISR内部是怎么做的? 事件组使用的场景 学校组织秋游,组长在等待: 张三:我到了 李四:我到了 王五:我到了 组长说:好,大家都到齐了,出发! 秋游回来第二天就要提交一篇心得报告,组长在焦急等待:张三、李四、王五谁先写好就交谁的

CentOS下mysql数据库data目录迁移

https://my.oschina.net/u/873762/blog/180388        公司新上线一个资讯网站,独立主机,raid5,lamp架构。由于资讯网是面向小行业,初步估计一两年内访问量压力不大,故,在做服务器系统搭建的时候,只是简单分出一个独立的data区作为数据库和网站程序的专区,其他按照linux的默认分区。apache,mysql,php均使用yum安装(也尝试

使用Spring Boot集成Spring Data JPA和单例模式构建库存管理系统

引言 在企业级应用开发中,数据库操作是非常重要的一环。Spring Data JPA提供了一种简化的方式来进行数据库交互,它使得开发者无需编写复杂的JPA代码就可以完成常见的CRUD操作。此外,设计模式如单例模式可以帮助我们更好地管理和控制对象的创建过程,从而提高系统的性能和可维护性。本文将展示如何结合Spring Boot、Spring Data JPA以及单例模式来构建一个基本的库存管理系统

Vue day-03

目录 Vue常用特性 一.响应更新 1. 1 v-for更新监测 1.2 v-for就地更新 1.3 什么是虚拟DOM 1.4 diff算法更新虚拟DOM 总结:key值的作用和注意点: 二.过滤器 2.1 vue过滤器-定义使用 2.2 vue过滤器-传参和多过滤器 三. 计算属性(computed) 3.1 计算属性-定义使用 3.2 计算属性-缓存 3.3 计算属

15 组件的切换和对组件的data的使用

划重点 a 标签的使用事件修饰符组件的定义组件的切换:登录 / 注册 泡椒鱼头 :微辣 <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-

12C 新特性,MOVE DATAFILE 在线移动 包括system, 附带改名 NID ,cdb_data_files视图坏了

ALTER DATABASE MOVE DATAFILE  可以改名 可以move file,全部一个命令。 resue 可以重用,keep好像不生效!!! system照移动不误-------- SQL> select file_name, status, online_status from dba_data_files where tablespace_name='SYSTEM'

rtklib.h : RTKLIB constants, types and function prototypes 解释

在 RTKLIB 中,rtklib.h 是一个头文件,包含了与 RTKLIB 相关的常量、类型和函数原型。以下是该头文件的一些常见内容和翻译说明: 1. 常量 (Constants) rtklib.h 中定义的常量通常包括: 系统常量: 例如,GPS、GLONASS、GALILEO 等系统的常量定义。 时间常量: 如一年、一天的秒数等。 精度常量: 如距离、速度的精度标准。 2. 类型

Weibull概率分布纸(EXCEL VBA实现)

在学习Weibull分布理论的时候,希望有一张Weibull概率纸,用来学习图解法。但是在度娘上没有找到的Weibull概率纸的电子版。在书上看到的Weibull概率纸,只能复印下来使用。于是萌生了自己制作Weibull概率纸的想法,帮助自己更好地学习。 本人擅长使用各种计算机语言,C,C++,Matlab,Scilab等等,但是始终钟爱与VBA,认为VBA可以实现一切你想要的东西,由于在企业里不